如何直接从列表中的所有嵌套列表中选择同一列? [英] How to directly select the same column from all nested lists within a list?
问题描述
是否可以直接选择列表中所有嵌套列表的列?
我的列表是通过使用table()的aggregate()创建的:
is it possible to directly select a column of all nested lists within a list?
My list is created using aggregate() with table():
AgN=aggregate(data,by=list(d$date),FUN=table,useNA="no")
AgN$x
看起来像:
$`0`
1 2 3 9 11
0.447204969 0.438509317 0.096894410 0.009937888 0.007453416
$`1`
1 2 4 8 11
0.489974937 0.389724311 0.102756892 0.006265664 0.011278195
…
$n
我想获取每个表的特定列的向量,例如一个包含所有名为"1"的列的值的向量.
我仍然是R的初学者,但是即使经过长时间的搜索和尝试,我仍然没有找到合适的解决方案.如果我想获取列表的字段,可以简单地用方括号将其编入索引,例如[i,j].
在网上我找到了一些矩阵示例,所以我尝试做同样的事情,起初只用AgN$x[1][1]
选择一个嵌套列表的列,但仍在选择整个列表:
I want to get a vector of a specific column of each table, e.g. a vector containing the values of all columns named "1".
I am still a R beginner, but even after searching and trying for a long time I found no nice solution. If I want to get the field of a list, I can simply index it with brackets, e.g. [i,j].
Online I found some examples for matrices, so I tried to do the same, at first only selecting one nested list’s column with AgN$x[1][1]
, but that is still selecting a whole list:
$
0
1 2 3 8 11
0.447204969 0.438509317 0.096894410 0.009937888 0.007453416
0.447204969 0.438509317 0.096894410 0.009937888 0.007453416
我的下一个尝试是AgN$x[[1]][1]
,它正在工作:
My next try was AgN$x[[1]][1]
, and it was working:
1
0.447205
所以我试图选择所有嵌套列表的第一列的值:
So I tried to to the same to select the value of each first column of all nested lists:
AgN$x[[1:length(AgN$x]][1]
Recursive indexing failed at level 2
显然,问题在于如果使用双括号,则不能选择范围.
Appearently the problem is that it is forbidden to select a range if you use a double brackets.
我最后的尝试是使用一个for循环:
My last try was to use an for loop:
cduR=NULL
for (i in 1:length(AgN$x)){
t=AgN$x[[i]]
cduR=c(cduR,as.vector(t["1"]))
}
最后,到目前为止,这似乎可行.但是那样的话,每次选择列时,我都必须建立一个循环.有没有直接的方法?
Finally, so far that seems to working. But that way I had to build a loop each time when I want to select columns. Is there no direct way?
感谢您的帮助.
推荐答案
假定您具有以下内容:
myList <- list(`0` = c(`1` = 10, `2` = 20, `3` = 30, `4` = 72),
`1` = c(`1` = 15, `2` = 9, `3` = 7))
myList
# $`0`
# 1 2 3 4
# 10 20 30 72
#
# $`1`
# 1 2 3
# 15 9 7
使用sapply()
或lapply()
进入列表并提取所需的任何列.一些例子.
Use sapply()
or lapply()
to get into your list and extract whatever columns you want. Some examples.
# As a list of one-column data.frames
lapply(myList, `[`, 1)
# $`0`
# 1
# 10
#
# $`1`
# 1
# 15
# As a list of vectors
lapply(myList, `[[`, 1)
# $`0`
# [1] 10
#
# $`1`
# [1] 15
# As a named vector
sapply(myList, `[[`, 1)
# 0 1
# 10 15
# As an unnamed vector
unname(sapply(myList, `[[`, 1))
# [1] 10 15
也可以帮助您到达该语法的其他变体包括:
Other variants of the syntax that also get you there include:
## Same output as above, different syntax
lapply(myList, function(x) x[1])
lapply(myList, function(x) x[[1]])
sapply(myList, function(x) x[[1]])
unname(sapply(myList, function(x) x[[1]]))
一个嵌套列表示例
如果要做具有嵌套列表(列表中的列表),请尝试以下变体.
A Nested List Example
If you do have nested lists (lists within lists), try the following variants.
# An example nested list
myNestedList <- list(A = list(`0` = c(`1` = 10, `2` = 20, `3` = 30, `4` = 72),
`1` = c(`1` = 15, `2` = 9, `3` = 7)),
B = list(`0` = c(A = 11, B = 12, C = 13),
`1` = c(X = 14, Y = 15, Z = 16)))
# Run the following and see what you come up with....
lapply(unlist(myNestedList, recursive = FALSE), `[`, 1)
lapply(unlist(myNestedList, recursive = FALSE), `[[`, 1)
sapply(unlist(myNestedList, recursive = FALSE), `[[`, 1)
rapply(myNestedList, f=`[[`, ...=1, how="unlist")
请注意,对于lapply()
和sapply()
,您需要使用unlist(..., recursive = FALSE)
,而对于rapply()
(递归应用),则直接引用该列表.
Note that for lapply()
and sapply()
you need to use unlist(..., recursive = FALSE)
while for rapply()
(recursive apply), you refer to the list directly.
这篇关于如何直接从列表中的所有嵌套列表中选择同一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!