I have the following three data frames:
df1 <- data.frame(A = 1:10, B = 3:12, D = 4:13)
df2 <- data.frame(C = 1:5, B = 4:8, E = 2:6)
df3 <- data.frame(A = 13:10, B = 19:16, F = 1:4)
rownames(df1) <- paste0("row", seq_len(nrow(df1)))
rownames(df2) <- paste0("row", c(1, 3, 5, 7, 11))
rownames(df3) <- paste0("row", c(12, 3, 10, 9))
# > df1
# A B D
# row1 1 3 4
# row2 2 4 5
# row3 3 5 6
# row4 4 6 7
# row5 5 7 8
# row6 6 8 9
# row7 7 9 10
# row8 8 10 11
# row9 9 11 12
# row10 10 12 13
# > df2
# C B E
# row1 1 4 2
# row3 2 5 3
# row5 3 6 4
# row7 4 7 5
# row11 5 8 6
# > df3
# A B F
# row12 13 19 1
# row3 12 18 2
# row10 11 17 3
# row9 10 16 4
When cells from different data frames have the same row and column names, I want to sum their values. In the cases where a cell has no matches (there aren't any other cells with the same row and column name), the final data frame will contain that original value. When a cell with a particular row name and column name combination doesn't exist in any of the original data frames, the final data frame will contain an NA in that position.
The final data frame should look like this data frame:
> df4
A B C D E G
row1 1 7 1 4 2 NA
row2 2 4 NA 5 1 NA
row3 15 28 2 6 5 2
row4 4 6 NA 7 3 NA
row5 5 13 3 8 8 NA
row6 6 8 NA 9 5 NA
row7 7 16 4 10 11 NA
row8 8 10 NA 11 7 NA
row9 19 27 NA 12 8 4
row10 21 29 NA 13 9 3
row11 NA 8 5 NA 6 NA
row12 13 19 NA NA NA 1
I'm imagining something with the Reduce() function that can be used on many data frames at once. Is the first step adding missing rows and columns to existing data frames with NAs in all the cells where values are missing?
Thanks!