Home > Net >  Filter data frame based off two columns in other data frame
Filter data frame based off two columns in other data frame

Time:04-08

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.

CodePudding user response:

Using %in%

dfZero <- df[df$Username %in% key[key$training == 0, "username"],]
dfOne <- df[df$Username %in% key[key$training == 1, "username"],]

Using merge()

dfZero <- merge(df, key[key$training == 0,], by.x = "Username", by.y = "username")
dfOne <- merge(df, key[key$training == 1,], by.x = "Username", by.y = "username")

CodePudding user response:

Using dplyr:

library(dplyr)

dflist <- merge(df, key, by.x = "Username", by.y = "username") %>%
  unique() %>%
  group_by(training) %>%
  group_split() 
  •  Tags:  
  • r
  • Related