Reputation:
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.
Upvotes: 0
Views: 406
Reputation: 21982
How about this:
library(dplyr)
renameddat <- renameddat %>% mutate(Education =
case_when(
Education %in% c(840001,840002) ~ "No GED",
Education == 840003 ~ "GED",
Education %in% c(840004,840005,840006) ~ "Undergraduate",
Education %in% c(840007,840008,840009) ~ "Graduate",
TRUE ~ NA_character_),
Education=factor(Education,
levels=c("No GED", "GED", "Undergraduate", "Graduate")))
Upvotes: 1
Reputation: 10996
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
Upvotes: 1