Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何直接从列表中的所有嵌套列表中选择同一列?_R_Nested Lists - Fatal编程技术网

R 如何直接从列表中的所有嵌套列表中选择同一列?

R 如何直接从列表中的所有嵌套列表中选择同一列?,r,nested-lists,R,Nested Lists,是否可以直接选择列表中所有嵌套列表的列? 我的列表是使用“与表聚合”创建的: 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

是否可以直接选择列表中所有嵌套列表的列? 我的列表是使用“与表聚合”创建的:

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]的嵌套列表列,但这仍然是选择一个完整的列表:

0美元

0.447204969 0.438509317 0.096894410 0.009937888 0.007453416

我的下一次尝试是AgN$x[[1]][1],它起了作用:

0.447205

因此,我尝试使用相同的值来选择所有嵌套列表中每个第一列的值:

AgN$x[[1:length(AgN$x]][1]
Recursive indexing failed at level 2
问题显然是,如果使用双括号,则禁止选择范围

我最后一次尝试是使用for循环:

cduR=NULL 
for (i in 1:length(AgN$x)){
t=AgN$x[[i]]
cduR=c(cduR,as.vector(t["1"]))
}
最后,到目前为止,这似乎是可行的。但这样一来,每当我想要选择列时,我都必须构建一个循环。没有直接的方法吗


谢谢您的帮助。

假设您有以下内容:

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 
语法的其他变体也可以帮助您实现这一目标,包括:

## 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]]))
嵌套列表示例 如果列表中确实有嵌套列表,请尝试以下变体

# 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")

请注意,对于Lappy和sapply,您需要使用unlist…,recursive=FALSE,而对于Rappy recursive apply,您直接引用列表。

一个我认为没有明确列出但也有效的示例是,如果您有一个数据列表。frames、matrix、xts、zoo等,以及行和列名,则可以随后返回整行,具有以下语法的列或集合:

具有以下格式的对象的列表: 代码注释第一行索引为空,指定所有行 输出
请注意,如果您希望所有列都命名为1,而不一定是第一列,那么您的选择标准应该是1,而不是1。谢谢,这似乎正是我想要的。因此,[在R中被视为一个函数,因此我可以将其作为FUN参数传递给apply?1是x参数,由apply传递给[]-函数?@Lukas,这是正确的。[是一个函数,但需要倒勾:`[`因此它的工作原理与本例相同。我第一次遇到它时对它不太熟悉,所以我通常使用匿名函数,这在我的答案的其他变体部分中有演示。如果我想使用此代码选择多个列,有什么建议吗?谢谢!这很有用。不过,我认为我们可以压缩函数仅使用sapply,即SapplyListofIndicaticalObjects,`[`,50%,而不是Simplify2ArraylApplyListofIndicaticalObjects,`[`,50%
# 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
## 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]]))
# 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")
                           0%              1%             10%              50%              90%             99%            100%
Sec.1           -0.0005259283   -0.0002644018   -0.0001320010   -0.00005253342    0.00007852480    0.0002375756    0.0007870917
Sec.2           -0.0006620675   -0.0003931340   -0.0001588773   -0.00005251963    0.00007965378    0.0002121163    0.0004190017
Sec.4           -0.0006091183   -0.0003994136   -0.0001859032   -0.00005230263    0.00010592379    0.0003165986    0.0007870917
Sec.8           -0.0007679577   -0.0005321807   -0.0002636040   -0.00005232452    0.00014492480    0.0003930241    0.0007870917
Sec.16          -0.0009055318   -0.0007448356   -0.0003449334   -0.00005290166    0.00021238287    0.0004772207    0.0007870917
Sec.32          -0.0013007873   -0.0009552231   -0.0005243472   -0.00007836480    0.00028928104    0.0007382848    0.0013002350
Sec.64          -0.0016409500   -0.0012383696   -0.0006617173   -0.00005280668    0.00042354939    0.0011721508    0.0018579966
Sec.128         -0.0022575471   -0.0018858823   -0.0008466965   -0.00005298436    0.00068616576    0.0014665900    0.0027616991
simplify2array(lapply(listOfIdenticalObjects,`[`,,"50%"))
                     ListItem1        ListItem2        ListItem3        ListItem4         ListItem5
Sec.1           -0.00005253342   -0.00004673443    -0.0001112780   -0.00001870960    -0.00002051009
Sec.2           -0.00005251963   -0.00004663200    -0.0001112904   -0.00001878075     0.00000000000
Sec.4           -0.00005230263   -0.00004669297    -0.0001112780   -0.00001869911    -0.00002034403
Sec.8           -0.00005232452   -0.00004663635    -0.0001111296   -0.00001926096     0.00000000000
Sec.16          -0.00005290166   -0.00004668207    -0.0001109570    0.00000000000     0.00000000000
Sec.32          -0.00007836480    0.00000000000    -0.0001111667   -0.00001894496     0.00000000000
Sec.64          -0.00005280668    0.00000000000    -0.0001110926   -0.00001878305     0.00000000000
Sec.128         -0.00005298436    0.00004675191     0.0000000000   -0.00005582568     0.00001020502