Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

merge by row.name and column

Tags:

merge

dataframe

r

This should be simple an I am probably just being silly but... I need to merge two data frames by the row names of df1 and a column in df2 i.e.

df1<-data.frame(x=1:3,y=4:6) 
rownames(df1)<-c("a","b","c")
df1
  x y
a 1 4
b 2 5
c 3 6

df2<-data.frame(site=c("a","b"),p=5:6,q=10:11)
df2
  site p  q
     a 5 10
     b 6 11

The merge should produce:

df3<-data.frame(site=c("a","b"),p=5:6,q=10:11,x=1:2,y=4:5)
df3
  site p  q x y
     a 5 10 1 4
     b 6 11 2 5

I have tried

merge(df1,df2,by.x=row.names(df1),by.y=df2$site) 

but get the error

Error in fix.by(by.x, x) : 'by' must specify uniquely valid column(s)

What am I doing wrong?

like image 645
Elizabeth Avatar asked Sep 11 '12 09:09

Elizabeth


People also ask

How do I merge Dataframes by row names in R?

The merge() function in base R can be used to merge input dataframes by common columns or row names. The merge() function retains all the row names of the dataframes, behaving similarly to the inner join. The dataframes are combined in order of the appearance in the input function call.

How do I merge datasets horizontally in R?

To merge two data frames (datasets) horizontally, use the merge function. In most cases, you join two data frames by one or more common key variables (i.e., an inner join).

How do I merge two rows in a Dataframe in R?

To merge two data frames (datasets) horizontally, use the merge() function in the R language. To bind or combine rows in R, use the rbind() function. The rbind() stands for row binding.


1 Answers

Here is one option:

merge(df1, df2, by.x = "row.names", by.y = "site")
  Row.names x y p  q
1         a 1 4 5 10
2         b 2 5 6 11
like image 150
A5C1D2H2I1M1N2O1R2T1 Avatar answered Sep 30 '22 16:09

A5C1D2H2I1M1N2O1R2T1