I have 5 dataframes each with a different number of rows. I need to merge them all together based on values in col1 of each dataframe. Observe
df1 <- read.table(text="
col1 col2
A 5
B 3
C 6
E 7", header=TRUE, stringsAsFactors=FALSE)
df2 <- read.table(text="
col1 col2
A 5
B 6
C 7
M 8
Z 9", header=T, stringsAsFactors=FALSE)
But I need it to produce:
newdf
col1 col2(#from df1) col3(#from df2)
A 5 5
B 3 6
C 6 7
E 7 0
M 0 8
Z 0 9
I have tried to merge a few at a time by='col1' but to no luck. Any tips?
What I have tried:
posidf<-merge(df1,df2,df3,df4,df5,all.x=TRUE)
#wont execute
posidf<-merge(df1,df2,df3,df4,df5,by="col1",all.x=TRUE)
#wont execute
posidf<-merge(df1,df2,df3,df4,df5,by="col1")
Error in fix.by(by.x, x) :
'by' must specify one or more columns as numbers, names or logical
all=parameter?merge()only takes two data.frames. You cannot pass in an arbitrary number of them? Check out the?mergehelp page. You would have to repeatedly call something likemerge(merge(merge(a,b), c, d). Did you try it with just two data frames like the example you actually included in your question?