使用gsub重新组织教育,但发生错误

我想重新组合“教育班”。例如,我想在高中之前就聚在一起。我分配了“ beforeHS”,但是输出不是我期望的。

原始输出:

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',data$education)
data$education <-gsub('^11th',data$education)
data$education <-gsub('^1st-4th',data$education)
data$education <-gsub('^5th-6th',data$education)
data$education <-gsub('^7th-8th',data$education)
data$education <-gsub('^9th',data$education)
data$education <-gsub('^Preschool',data$education)
data$education<-as.factor(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 

我确实看到了“ beforeHS”,但是所有

jayxh314 回答:使用gsub重新组织教育,但发生错误

由于您要转到"beforeHS"的所有值都以数字(“ 1st”,“ 5th”,“ 11th”)开头或为“学龄前”。我们可以创建一个模式来检测此情况,并用"beforeHS"替换这些值。

data$education[grepl("^\\d|Preschool",data$education)] <- "beforeHS"
本文链接:https://www.f2er.com/3106245.html

大家都在问