我想将一个具有缺失列的命名列表转换为数据框。我可以使用已弃用的rbind_all
成功完成,但不能使用替代方法bind_rows
。
例子
缺少列的列表(el3
缺少b
)
ex = list(el1=c(a=1, b=2, c=3), el2=c(a=2, b=3, c=4), el3=c(a=3, c=5))
rbind_all(ex)
# A tibble: 3 x 3
a b c
<dbl> <dbl> <dbl>
1 1 2 3
2 2 3 4
3 3 NA 5
> bind_rows(ex)
Error in bind_rows_(x, .id) : Argument 3 must be length 3, not 2
没有丢失的列
ex2 = list(el1=c(a=1, b=2, c=3), el2=c(a=2, b=3, c=4), el3=c(a=3, b=4, c=5))
rbind_all(ex2)
# A tibble: 3 x 3
a b c
<dbl> <dbl> <dbl>
1 1 2 3
2 2 3 4
3 3 4 5
bind_rows(ex2) # Output is transposed for some reason
# A tibble: 3 x 3
el1 el2 el3
<dbl> <dbl> <dbl>
1 1 2 3
2 2 3 4
3 3 4 5
如何使用非弃用的函数复制
rbind_all
的行为?
do.call(bind_rows, ex)
? - Danbind_rows
“数据框、可能是数据框的列表或数据框的列表”。 - Ritchie Sacramento