Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- df <- data.frame(status=c("Domestic partners (w/children)", "Married (no
- children)", "Single (no children)"))
- df$married <- sapply(strsplit(as.character(df$status) , " \(") , "[" , 1)
- # Change to factor
- df$married <- factor(df$married , levels=c("Single" , "Married",
- "Domestic partners"))
- df$ch <- ifelse(grepl("no children" , df$status) , 0 , 1)
- s <- strsplit(as.character(df$status) , " \(")
- sapply(s , "[" , 1)
- grepl("no children" , df$status)
- df <- data.frame(status=c("Domestic partners (w/children)", "Married
- (no children)", "Single (no children)",""))
- df$ch[df$status==""] <- NA
- df$ch[is.na(df$status)] <- NA
- # status married ch
- # 1 Domestic partners (w/children) Domestic partners 1
- # 2 Married (no children) Married 0
- # 3 Single (no children) Single 0
- # 4 <NA> NA
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement