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?
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.
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).
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With