Skip to content

Instantly share code, notes, and snippets.

@cmacrander
Created February 8, 2016 18:08
Show Gist options
  • Save cmacrander/d06313468332f6019d5e to your computer and use it in GitHub Desktop.
Save cmacrander/d06313468332f6019d5e to your computer and use it in GitHub Desktop.
q_unique_ed <- mutate(
q_unique,
#the reason for the repetitions is that I cannot figure out how to handle the NAs otherwise
mother_education = t1__demog__ed_level_m,
father_education = t1__demog__ed_level_f,
mother_education = ifelse(mother_education > 10,NA,mother_education),
father_education = ifelse(father_education > 10,NA,father_education),
mother_education = ifelse(is.na(mother_education),0,mother_education),
father_education = ifelse(is.na(father_education),0,father_education),
highest_parents_education = apply(data.frame(mother_education,father_education), 1, max),
highest_parents_education = ifelse(highest_parents_education %in% 0, NA, highest_parents_education),
generation = highest_parents_education,
generation = ifelse(generation > 4,"continuing","first"),
generation = ifelse(is.na(generation),"unknown",generation)
)
############
get_parent_ed <- function (ed_level) {
if (ed_level > 10 | is.na(ed_level)) {
0
} else {
ed_level
}
}
get_generation <- function (highest_parents_ed) {
if (is.na(highest_parents_ed)) {
"unknown"
} else if (highest_parents_ed > 4) {
"continuing"
} else {
"first"
}
}
q_unique_ed <- q_unique %>%
rowwise() %>%
mutate(
mother_education = get_parent_ed(t1__demog__ed_level_m),
father_education = get_parent_ed(t1__demog__ed_level_f),
highest_parents_education = max(mother_education, father_education),
generation = get_generation(highest_parents_education)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment