Reputation: 1182
I have read data from 3 different files into 3 different dataframes. I have tried merge function but doesnot seem to give me the desired output. My dataframes are:
df1:
someName someMOD someID
A T754(P),M691(O),S692(P),S694(P),S739(P),S740(P),S759(P),S762(P) 1
B S495(P) 2
C S162(P),Q159(D) 3
D S45(P),C47(C),S48(P),S26(P) 4
E S18(P) 5
df2:
someName someMOD someID
C S162(P),Q159(D) 3
D S45(P),C47(C),S48(P),S26(P) 4
F S182(P) 6
E S18(P) 5
Z Q100(P) 9
A T754(P),M691(O),S694(P),S739(P),S740(P) 1
df3:
someName someMOD someID
A T754(P),M691(O),S692(P),S694(P),S739(P),S740(P),S759(P) 1
B S495(P) 2
D S45(P),C47(C),S48(P),S26(P) 4
E S18(P) 5
F S182(P) 6
L Z182(P) 8
C S162(P),Q159(D) 3
I would like an output like the following which is merged by someID column:
Any help is really appreciated.Thanks.
Upvotes: 3
Views: 3016
Reputation: 647
Have you tried this function :
http://rss.acs.unt.edu/Rdoc/library/gtools/html/smartbind.html
See this code in the link:
out <- smartbind( list(df1, df2, ...))
Upvotes: 0
Reputation: 8763
library(data.table)
dt1 <- data.table(df1, key="someName,someID")
dt2 <- data.table(df2, key="someName,someID")
dt3 <- data.table(df3, key="someName,someID")
DT <- dt1[dt2[dt3]]
Upvotes: 2
Reputation: 57697
Use Reduce
to merge multiple data frames together. One annoying thing about Reduce
is that it passes the underlying function exactly 2 arguments, so you have to work around that.
mymerge <- function(x, y)
merge(x, y, by=c("someName", "someID"), all=TRUE))
Reduce(mymerge, list(df1, df2, df3))
Upvotes: 3