Reputation:
I have a very simple problem but I can't figure it out. I've been trying for hours and previous read Stackoverflow answers but none of them addresses directly my problem. I have to append columns to a dataframe dynamically. Let's say I have:
df1 <- data.frame (c("a","b","c","d"))
df2 <- data.frame (c("aa", "bb", "ccc"))
df3 <- data.frame (c("aaa", "bbb"))
df4 <- data.frame (c("aaaa"))
df5 <- data.frame ()
Notice that none them has a the same number of rows, nor have primary keys values. And one them is empty.
How can I merge these 5 dataframes by their coordinates in one dataframe df6
, filling the missing values with N/A
?
To be more specific, I want the final output to be like: df6
a aa aaa aaaa N/A
b bb bbb N/A N/A
c cc N/A N/A N/A
d N/A N/A N/A N/A
Upvotes: 1
Views: 54
Reputation: 389315
You can use cbind.fill
function from rowr
package with fill
option as NA
library(rowr)
cbind.fill(df1, df2, df3, df4, df5, fill = NA)
1 a aa aaa aaaa NA
2 b bb bbb <NA> NA
3 c ccc <NA> <NA> NA
4 d <NA> <NA> <NA> NA
Upvotes: 1