Rbind数组的名称

时间:2016-03-09 16:19:09

标签: r dataframe rbind

我有两个命名数组,可能有不同的长度

row1<-c(a="a1",b="b1",c="c1")
row2<-c(a="a1",d="d1",b="b2",c="c2")

我想绑定它们维护名称的顺序,结果应该是:

      a    d    b    c   
row1 "a1"  NA  "b1" "c1"
row2 "a1" "d1" "b2" "c2"

但是使用通常的 rbind 结果是

       a    d    b    c   
row1 "a1" "b1" "c1" "a1"
row2 "a1" "d1" "b2" "c2"

有什么建议吗?

谢谢

1 个答案:

答案 0 :(得分:3)

我建议

n = sort(union(names(row1),names(row2)))
rbind(row1[n], row2[n])

#      a    b    c    <NA>
# [1,] "a1" "b1" "c1" NA  
# [2,] "a1" "b2" "c2" "d1"

如果涉及很多行,您可以执行

myrows = list(row1,row2)
n = sort(Reduce(union, lapply(myrows, "names")))
do.call(rbind, lapply(myrows, `[`, n))

问题用data.frame标记,但该示例使用矩阵。使用data.frames,我会使用其中一个包:

# new example
r1<-data.frame(a="a1",b="b1",c="c1", stringsAsFactors=FALSE)
r2<-data.frame(a="a1",d="d1",b="b2",c="c2", stringsAsFactors=FALSE)

library(data.table)
rbindlist(list(r1,r2), fill=TRUE)

#     a  b  c  d
# 1: a1 b1 c1 NA
# 2: a1 b2 c2 d1

library(dplyr)
bind_rows(r1, r2)

#       a     b     c     d
#   (chr) (chr) (chr) (chr)
# 1    a1    b1    c1    NA
# 2    a1    b2    c2    d1

同样,如果涉及许多行rs = list(r1,r2),您可以执行rbindlist(rs, fill=TRUE)do.call(bind_rows, rs)