This question is a follow-up to this one, which got negative responses and no answers. So, I'm trying to do this using R.
I have many (more than 30) files like this:
File1
5 A1 23 A3 1 B2 289 BX5 90 D3
File2
2 A1 10 A2 3 B1 1 BX4 90 D3 24 E0
File3
4 A0 11 A2 1 B1 2 D3
And I would like to combine all of them to produce a data frame like this:
A0 0 0 4
A1 5 2 0
A2 0 10 11
A3 23 0 0
B1 0 3 1
B2 1 0 0
BX4 0 1 0
BX5 289 0 0
D3 90 90 2
E0 0 24 0
Based on this, I tried to read two files using read.table while specifying the second column as the row names and then I merged the data frames by row names, like this:
> df1 <- read.table("File1", row.names = 2)
> df1
V1
A1 5
A3 23
B2 1
BX5 289
D3 90
> df2 <- read.table("File2", row.names = 2)
> df2
V1
A1 2
A2 10
B1 3
BX4 1
D3 90
E0 24
> m1 <- merge(df1, df2, by=0, all=TRUE)
> m1[is.na(m1)] <- 0
> m1
Row.names V1.x V1.y
1 A1 5 2
2 A2 0 10
3 A3 23 0
4 B1 0 3
5 B2 1 0
6 BX4 0 1
7 BX5 289 0
8 D3 90 90
9 E0 0 24
So far so good, but when I tried to merge the resulting data frame to the third one, it doesn't work as I hoped for. And because of that, I'm not sure how I will continue to merge all the 30-something files into one data frame. Previously I thought I would modify the multmerge
function described here, but now I'm stuck.
So, would anybody please help me with this? Thanks in advance.
EDIT: I would also really appreciate if anyone could suggest me a better title for this question.