I am using survey data from the World Values Survey, I used the code below to change my variable from a numeric to an ordered variable
renameddata$Education= ordered(renameddata$Education, levels =c(-2,-1,840001,840002,840003,
840004,840005,840006,840007,
840008,840009),
labels = c("NA","NA","LessHighSchool","SomeHighSchool",
"GED","SomeCollege","Associates","Bachelors",
"Masters","Professional","Doctorate"))
However, now I want to recode the education variable so that LessHighSchool
and SomeHighSchool
become one e.g "NO GED"
, and so that SomeCollege
, Associates
and Bachelors
become "Undergraduate"
etc.
Alternatively, if you want to recode the created factor variable, you can use fct_collapse
from the forcats
package:
Input:
renameddata <- data.frame(Education = c(-2, -1, 840001, 840002, 840003, 840004, 840005, 840006, 840007, 840008, 840009))
renameddata$Education = ordered(renameddata$Education,
levels = c(-2, -1, 840001, 840002, 840003, 840004, 840005, 840006, 840007, 840008, 840009),
labels = c("NA", "NA", "LessHighSchool", "SomeHighSchool", "GED", "SomeCollege", "Associates", "Bachelors", "Masters", "Professional", "Doctorate"))
Recoding:
library(forcats)
renameddata$Education <- fct_collapse(renameddata$Education,
"NO GED" = c("LessHighSchool", "SomeHighSchool"),
"Undergraduate" = c("SomeCollege", "Associates", "Bachelors"))
gives:
Education
1 NA
2 NA
3 NO GED
4 NO GED
5 GED
6 Undergraduate
7 Undergraduate
8 Undergraduate
9 Masters
10 Professional
11 Doctorate