R 使用gsub重新组合教育,但出现错误
我想重组“教育班”。例如,我想在高中前一起分组。我分配“beforeHS”,但输出不是我期望的 原始输出:R 使用gsub重新组合教育,但出现错误,r,class,group-by,grouping,rename,R,Class,Group By,Grouping,Rename,我想重组“教育班”。例如,我想在高中前一起分组。我分配“beforeHS”,但输出不是我期望的 原始输出: summary(data$education) 11th 5th-6th 7th-8th 9th Assoc-acdm Assoc-voc 1175 333 646 514 1067 1382
summary(data$education)
11th 5th-6th 7th-8th 9th Assoc-acdm Assoc-voc
1175 333 646 514 1067 1382
Bachelors Doctorate HS-grad Masters Preschool Prof-school
5354 413 10501 1723 51 576
Some-college 12th beforeHS Unknown
7291 168 433 933
下面,我尝试使用gsub()函数
###combine high school below or 12th together
data$education <-gsub('^12th', 'beforeHS', data$education)
data$education <-gsub('^10th', 'beforeHS', data$education)
data$education <-gsub('^11th', 'beforeHS', data$education)
data$education <-gsub('^1st-4th', 'beforeHS', data$education)
data$education <-gsub('^5th-6th', 'beforeHS', data$education)
data$education <-gsub('^7th-8th', 'beforeHS', data$education)
data$education <-gsub('^9th', 'beforeHS', data$education)
data$education <-gsub('^Preschool', 'beforeHS', data$education)
data$education<-as.factor(data$education)
我确实看过“beforeHS”,但所有
“beforeHS”
的值要么以数字(“1”、“5”、“11”)开头,要么以“preforehs”开头。我们可以创建一个模式来检测这一点,并将这些值替换为“beforeHS”
data$education[grepl(“^\\d| predure”,data$education)]您可能需要提供一个可复制的示例。我看不出你的代码有什么不工作的原因(看起来只有第一行代码不工作,没有其他原因)。例如,尝试在iris
数据集的副本上运行类似的代码是可行的。(除此之外,还有更简单的方法来执行您正在执行的操作;考虑到您基本上是在执行整个字符串的精确匹配,gsub
在这里并不是真正必要的)我猜您的$education
有前导空格:通常这些列的宽度比最宽的字符串多一个。由于最宽为12个字符,但列之间的间距似乎为14,这表明至少最长字符串上至少有一个前导空格。。。如果其中有前导空格,请检查所有空格。或者直接运行data$education,非常感谢。存在前导空间,我可以在修剪空间后重新组合。
summary(data$education)
11th 5th-6th 7th-8th 9th Assoc-acdm Assoc-voc
1175 333 646 514 1067 1382
Bachelors Doctorate HS-grad Masters Preschool Prof-school
5354 413 10501 1723 51 576
Some-college beforeHS Unknown
7291 601 933
data$education[grepl("^\\d|Preschool", data$education)] <- "beforeHS"