Home > Software engineering >  how to select rows from one data frame based in other data frame R (dplyr)
how to select rows from one data frame based in other data frame R (dplyr)

Time:11-14

I have two data frames: df1 and df2

ID <- c('PD1', 'PD1', 'PD2', 'PD2')
run <- c('A1', 'A4', 'A6', 'A7')
df <- data.frame(ID=ID, run =run)

name <- c('n1', 'n1', 'n1','n1', 'n1', 'n1')
run <- c('A1', 'A2', 'A3', 'A4', 'A5', 'A6')
value <- c(4.5, 6.7, 0.9, 8.2, 5.7, 9.4)
df2 <- data.frame(name =name, run=run, value = value)

and I would like to subset my df2 based on the values in df1 to obtain this result

name run  value
n1  A1   4.5
n1  A4   8.2
n1  A6   9.4

I've tried this

NewDataframe <- merge(df2, df1 , by= 'run',all.x=FALSE)

which works but I only one to keep the cols in df2

Any suggestion? Thank you

CodePudding user response:

Try this:

library(dplyr)

df2 %>% 
  filter(run %in% df$run)

Result

  name run value
1   n1  A1   4.5
2   n1  A4   8.2
3   n1  A6   9.4

CodePudding user response:

In base R you can subset like this:

df2[which(df2$run %in% df$run),]
  name run value
1   n1  A1   4.5
4   n1  A4   8.2
6   n1  A6   9.4

In dplyr you could use left_join:

left_join(df, df2, by = "run") %>% na.omit(.)
  • Related