我想将数据帧与相似的命名列组合在一起并引入NA,其中一个数据帧缺少列值(在这种情况下,df2中缺少z变量)。
>df1 <- data.frame(x = 1:10,
y = 1:10,
z = 1:10)
>df2 <- data.frame(y = 11:20,
x = 11:20)
#my output could look like this with NA's added where there are missing columns.
>data.frame(x = 1:20,
y = 1:20,
z = c(1:10, rep(NA, 10)))
答案 0 :(得分:4)
Hadley为此提供了一个很好的功能:
library(plyr)
rbind.fill(df1, df2)
# x y z
#1 1 1 1
#2 2 2 2
#3 3 3 3
#4 4 4 4
#5 5 5 5
#6 6 6 6
#7 7 7 7
#8 8 8 8
#9 9 9 9
#10 10 10 10
#11 11 11 NA
#12 12 12 NA
#13 13 13 NA
#14 14 14 NA
#15 15 15 NA
#16 16 16 NA
#17 17 17 NA
#18 18 18 NA
#19 19 19 NA
#20 20 20 NA