Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. df <- data.frame(status=c("Domestic partners (w/children)", "Married (no
  2. children)", "Single (no children)"))
  3.  
  4. df$married <- sapply(strsplit(as.character(df$status) , " \(") , "[" , 1)
  5.  
  6. # Change to factor
  7. df$married <- factor(df$married , levels=c("Single" , "Married",
  8. "Domestic partners"))
  9.  
  10. df$ch <- ifelse(grepl("no children" , df$status) , 0 , 1)
  11.  
  12. s <- strsplit(as.character(df$status) , " \(")
  13.  
  14. sapply(s , "[" , 1)
  15.  
  16. grepl("no children" , df$status)
  17.  
  18. df <- data.frame(status=c("Domestic partners (w/children)", "Married
  19. (no children)", "Single (no children)",""))
  20.  
  21. df$ch[df$status==""] <- NA
  22.  
  23. df$ch[is.na(df$status)] <- NA
  24.  
  25. # status married ch
  26. # 1 Domestic partners (w/children) Domestic partners 1
  27. # 2 Married (no children) Married 0
  28. # 3 Single (no children) Single 0
  29. # 4 <NA> NA
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement