Suppose I have three different data sets (practically I have more) with different variables but one which is ID
, I would like to merge them by IDs which are common to all data and keep all the columns.
df1=data.frame(ID=c(1:10), x1=rnorm(10), y1=rnorm(10), z1=rnorm(10))
df2=data.frame(ID=c(2:11),x2=rpois(10,3), y1=rbinom(10,5,0.64),z2=rbinom(10,1,0.80) )
df3=data.frame(ID=c(5:14),x3=rbinom(10,1,0.80), y3=rnorm(10),x3=rpois(10,4) )
same_id=Reduce(intersect,list(df1$ID, df2$ID, df3$ID))
> same_id
[1] 5 6 7 8 9 10
I would like to create a data set that will have only same_id
and their corresponding values for all variables.
Any help is appreciate