gpt4 book ai didi

rbind 具有不同列的数据框列表

转载 作者:行者123 更新时间:2023-12-05 00:49:37 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





do.call(rbind, list) for uneven number of column

(4 个回答)



Efficient way to rbind data.frames with different columns

(4 个回答)



Why is rbindlist "better" than rbind?

(2 个回答)


3年前关闭。




我有一个数据框列表,其中某些数据框的列数较少。见示例:

a <- data.frame (x1 = 1, x3 = 2, x4 = 7)
b <- data.frame (x1 = 3, x2 = 4, x3 = 3, x4 = 8)
c <- data.frame (x1 = 9, x2 = 5, x3 = 2, x4 = 9)
myList = list(a, b, c)

数据框 a遗漏专栏 x2 .我需要从 myList 中获取数据框,因此我执行以下操作:
mydf = do.call(rbind, myList)

但问题是我收到以下错误:
Error in rbind(deparse.level, ...) : 
numbers of columns of arguments do not match

如何获取列 x2 所在的数据框为 a充满 NA ?

最佳答案

您可以使用 data.table :

library(data.table)
rbindlist(myList, fill = TRUE)
# x1 x3 x4 x2
#1: 1 2 7 NA
#2: 3 3 8 4
#3: 9 2 9 5

关于rbind 具有不同列的数据框列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47378447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com