我想重新组合“教育班”。例如,我想在高中之前就聚在一起。我分配了“ 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”,但是所有