5

I have two dataframes. The row names in both are dates. What I want to do is, I want to select all the common rows (having same dates) in both the data frames and create a new data frame having only these common rows.

Of course the individual columns would get appended next to each other.

Can anyone please help??

Thomas
  • 43,637
  • 12
  • 109
  • 140
user2127116
  • 61
  • 1
  • 3

2 Answers2

5

Try:

merge(df1, df2, by="row.names")
?merge

Can also use by=0 instead of 'row.names'. And BTW the rownames are not R Date class, but are character valued. I suppose one could also do this:

 cbind( df1[ intersect(rownames(df1), rownames(df2)), ] ,
        df2[ intersect(rownames(df1), rownames(df2)), ] )
thelatemail
  • 91,185
  • 12
  • 128
  • 188
IRTFM
  • 258,963
  • 21
  • 364
  • 487
0

Based on BondedDust's Answer's, if you use the first line of his suggestion you can get the data like you want, since you will define an intersection of the data with the function "intersect",filter the data with the operators ('[' and ']') and bind the data by columns with the 'cbind' function.

cbind( df1[ intersect(rownames(df1), rownames(df2)), ])