How to directly select the same column from all nested lists within a list?

依然范特西╮ 提交于 2019-11-27 06:49:32

Assuming you have something like the following:

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 

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

Note that for lapply() and sapply() you need to use unlist(..., recursive = FALSE) while for rapply() (recursive apply), you refer to the list directly.

One example I don't think is explicitly listed but also works is if you have a list of data.frames, matrix, xts, zoo, etc with row and column names, you can subsequently return an entire row, column or collection with the following syntax:

List with objects of format:

                           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

Code (note the empty first row index, specifying all rows)

simplify2array(lapply(listOfIdenticalObjects,`[`,,"50%"))

Output

                     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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!