我在R中有一个列表的列表。我想将其转换为一个列表数组,但我只得到了一个列表的列表数组:
r <- list(list(a=1, b=NULL, c=NULL, d=1.23),
list(a=2, b=NULL, c=NULL, d=3.32),
list(a=3, b=NULL, c=NULL, d=2.13),
list(a=4, b=NULL, c=NULL, d=3.25),
list(a=5, b=NULL, c=NULL, d=0.14),
list(a=6, b=NULL, c=NULL, d=5.13))
x <- array(r, dim=c(3,2))
x[1,1] # now a list of length 1 containing an element which is a list with components a--d
如您所见,
x[1,1]
(例如)现在是一个列表的列表,但“外部”列表是无用的。我更想要一个数组y
,其中y[i,j]
为x[i,j][[1]]
。有什么最好的方法可以实现这一点(使用基本的R函数(无需额外包))?我尝试使用一些
unlist()
技巧,如array(unlist(r, recursive=FALSE), dim=c(3,2))
,但失败了。sapply(r, FUN=I)
至少给出了一个矩阵...也许那会有所帮助(?)
x[1,1][[1]]
,而这正是提问者想要避免使用的。 - Theodore Lytras