Guest User

Untitled

a guest
Oct 18th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. library(tidyverse)
  2.  
  3. set.seed(1234)
  4.  
  5. data <- c("10th Std. Pass", "11th Std. Pass", "12th Std. Pass", "5th Std. Pass",
  6. "6th Std. Pass", "Diploma / certificate course", "Graduate", "No Education")
  7.  
  8. education <- factor(sample(data, size = 5, replace = TRUE),
  9. levels = c(data, "Data not available"))
  10.  
  11. survey <- tibble(education)
  12.  
  13. recoded_s <- survey %>% mutate(education =
  14. fct_collapse(education,
  15. "None" = "No Education",
  16. "Primary" = c("5th Std. Pass", "6th Std. Pass"),
  17. "Secondary" = c("10th Std. Pass", "11th Std. Pass", "12th Std. Pass"),
  18. "Tertiary" = c("Diploma / certificate course", "Graduate")
  19. ))
  20.  
  21. recoded_s$education
  22. #> [1] Secondary Primary Primary Primary Tertiary
  23. #> Levels: Secondary Primary Tertiary None Data not available
  24.  
  25.  
  26. # Re-ordering and dropping variables
  27. factor(recoded_s$education, levels = c("None", "Primary", "Secondary", "Tertiary"))
  28. #> [1] Secondary Primary Primary Primary Tertiary
  29. #> Levels: None Primary Secondary Tertiary
Add Comment
Please, Sign In to add comment