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
>Solution :
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