I'm sure the answer to this will be VERY similar to this question but I just can't quite put it together.
I have two data frames. One is the data frame I'm working on:
df <- structure(list(Username = c("hmaens", "pgcmann",
"gsamse", "gsamse",
"gsamse", "gamse"),
Title = c("Pharmacy Resident PGY2",
"Associate Professor of Pediatrics",
"Regulatory Coordinator",
"Regulatory Coordinator",
"Regulatory Coordinator",
"Regulatory Coordinator"),
`User Role` = c("Investigational Pharmacist",
"Principal Investigator",
"Calendar Build",
"Protocol Management",
"Subject Management",
"Regulatory")),
row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
and one is they key:
key <- structure(list(username = c("hmaens", "pgcmann",
"gsamse", "gsamse",
"gsamse", "gsamse"),
training = c(0, 0, 1,
1, 1, 1)),
row.names = c(NA, -6L),
class = c("tbl_df", "tbl", "data.frame"))
I want to split my "df" data frame based on the "training" column in key. I.e. my results would be a data frame called dfZero with the exact same columns from df that had everyone from key with a "0" in training. And a separate data frame called dfOne with the 1's from key$training.
df %>% left_join(key, by=c("Username"="username")) %>% split(~training). That will give you a list with the two separate data.frames.gsameis not present inkey, so thattrainingisNA.NAvalues. An alternative is to usedplyr::nest_by(training)which will preserve them.dfZero <- df[df$username %in% key[key$training == 0, "username"],]