Last active
June 12, 2019 17:26
-
-
Save garethclews/83bbae15b0c27f42ab5694e6aac5ef5e to your computer and use it in GitHub Desktop.
R data manipulation request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Passing over a set of columns and passing out 0 || 1 depending on any value | |
# matching specified flag | |
library("tidyverse") # %>%, pmap, mutate, select | |
library("DescTools") # %like% | |
# I would very much like to use importFrom but don't have a NAMESPACE here | |
# putting this here just to clarify the mutate call below | |
is.parent <- function(xs) any(xs == "08") | |
# tibble/dataframe df created containing your data | |
# get all of the columns which match the pattern RELAT[something] in name | |
df[names(df) %like% "RELAT%"] %>% | |
# for each row create a 'parent' value if some of the variables satisfy in.parent | |
mutate(parent = pmap(., ~some(., is.parent))) %>% | |
# faff which takes just that column, turns it into an integer (as requested) | |
select(parent) %>% | |
map(as.integer) -> df$parent | |
# write it out to the parent variable of your df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment