【发布时间】:2017-01-11 16:29:19
【问题描述】:
这里生成的列表 x 列表如下:
list1 <- list(NULL, as.integer(0))
list2 <- list(NULL, as.integer(1))
list3 <- list(1:5, 0:4)
x <- list(a=list1, b=list2, c=list3)
x 的结构如下:
str(x)
List of 3
$ a:List of 2
..$ : NULL
..$ : int 0
$ b:List of 2
..$ : NULL
..$ : int 1
$ c:List of 2
..$ : int [1:5] 1 2 3 4 5
..$ : int [1:5] 0 1 2 3 4
我正在尝试将其转换为强制数据帧。我第一次使用
xc <- data.frame(lapply(x, as.numeric)
我收到以下错误
Error in lapply(x, as.numeric) :
(list) object cannot be coerced to type 'double
实际上它只适用于 as.character 作为参数。
我的目标是达到具有以下结构的数据框:
str(xc)
'data.frame': 2 obs. of 3 variables:
$ a: int NA 0 ...
$ b: int NA 1 ...
$ c: int [1:5] 1 2 3 4 5 int [1:5] 0 1 2 3 4
【问题讨论】:
-
试试
data.frame(lapply(x, unlist))请展示一个可重现的小例子 -
错误(函数(...,row.names = NULL,check.rows = FALSE,check.names = TRUE,:参数暗示不同的行数:1434、1081、5465、95263 , 18705, 6206, 12085, 22000, 499
-
好的,请展示一个使用
dput的可重现的小示例,因为对于大数据单独来自str的数据结构尚不清楚 -
我已经使用 do.call(rbind, x) 将列表转换为单个数据框,但没有示例我无法在这里进行测试。
-
在操作列表之前,您可能必须将
NULL替换为NA,以防止生成的数组长度不同。