1

I have been trying to create a dataframe (df3) based on two other dataframes of different sizes(df1, df2) and the following conditions:

Include in df3 all the rows where: df1$id = df2$id and df1$cond=df2$cond

I tried:

df3<-df1[(df1$id %in% df2$id &
            df1$fcond %in% df2$cond),] 

But it selects the rows where at least one of the arguments is true, instead of both.

Dummy data:

t<- c(1,2,3,4,5,6,7,8,9,10)
id<-c(1,1,2,2,3,3,4,4,5,5)
cond <- c(1,5,1,1,1,5,1,1)

df1<-data.frame(cbind(t,id,cond))

id <-c(1,4)
cond <-c(1,1)

df2<- data.frame(cbind(id, cond))

Expected output:

t<- c(1,7)
id<-c(1,4)
cond<- c(1,1)
df3< c(t, id, cond)

Any ideas?

Anna
  • 177
  • 13
  • Warning message: In cbind(t, id, cond) : number of rows of result is not a multiple of vector length (arg 3) – Hong Ooi Feb 08 '21 at 17:38

1 Answers1

1

May be we need

library(dplyr)
inner_join(df1, df2, by = c('id', 'cond'))
akrun
  • 874,273
  • 37
  • 540
  • 662