我试图“使用正则表达式来使用高度列来创建一个total_inches列,其总高度以英寸为单位,并被记录为一个数值变量。
我想出了一个解决办法..。但我不认为它适合这个任务..。(语言为R)
bball_data$total_inches <- bball_data$height %>%
str_extract(regex("(^\\d+) (-) (\\d+$)", comments = TRUE))
bball_data <- bball_data %>% separate(total_inches, c("feet", "inches"), "-", convert = TRUE) %>%
mutate(total_inches = (12*feet + inches))除了使用正则表达式外,最上面的一行基本上是无用的.已经三个小时了,我错过了什么?
*更新..。
bball_data$total_inches <- str_replace(bball_data$height,regex("(^\\d+)(-)(\\d+$)", comments = TRUE), "\\1+\\3")这让我离最终结果很近了。但你可能会猜到我不能加1比3.我试过as.numeric和as.int..。但两样都不管用..。还有其他方法来增加这个替换值吗?dput输出是..。
结构(list=c(Alaa Abdelnaby)、"Zaid Abdul-Aziz“、"Kareem Abdul-Jabbar”、"Mahmoud Abdul-Rauf“、"Tariq Abdul-Wahad”、"Shareef Abdur-Rahim")、year_start =c(1991年L、1969 L、1970L、1991 L、1998 L、1997 L)、year_end =c(1995 L、1978 L、1989 L、2001 L、2003 L、2008 L)、职位= c("F-C“、"C-F”、"C“、"G”、"F“、”F“、"F");身高= c("6-10“、"6- 9”、" 7 -2“、"6-1”、"6-6“、"6-9"),重量=c(240升、235升、225升、162升、223升、225升)、birth_date =c(”1968年6月24日“、1946年4月7日、”1947年4月16日“、”1969年3月9日“、”1974年11月3日“、”1976年12月11日“)、学院=c(”杜克大学“)“爱荷华州大学”、“加州大学洛杉矶分校”、“路易斯安那州立大学”、“圣何塞州立大学”、“加利福尼亚大学”),row.names = c(NA,6L),class = "data.frame")
发布于 2022-09-13 13:02:06
我很抱歉发布了一个糟糕的问题格式..。下次我会做得更好..。这是我在大约5.5小时后得出的答案!
bball_data$height <- str_extract_all(bball_data$height, "\\d+", simplify = TRUE)
bball_data$total_inches <- as.numeric(bball_data$height[,1])*12 + as.numeric(bball_data$height[,2])这个代码将获取高度,然后将其放入一个矩阵中。第2行将接受这个值as.numeric,然后计算我需要的值“总英寸”,并将其放入bball_data中的一个total_inches列中。
https://stackoverflow.com/questions/73696265
复制相似问题