Accessing same named list elements of the list of lists in R

后端 未结 3 1491
夕颜
夕颜 2020-12-24 09:20

Frequently I encounter situations where I need to create a lot of similar models for different variables. Usually I dump them into the list. Here is the example of dummy cod

相关标签:
3条回答
  • 2020-12-24 09:33

    I usually use kohske way, but here is another trick:

     sapply(modlist, with, rank)
    

    It is more useful when you need more elements, e.g.:

     sapply(modlist, with, c(rank, df.residual))
    

    As I remember I stole it from hadley (from plyr documentation I think).

    Main difference between [[ and with solutions is in case missing elements. [[ returns NULL when element is missing. with throw an error unless there exist an object in global workspace having same name as searched element. So e.g.:

    dah <- 1
    lapply(modlist, with, dah)
    

    returns list of ones when modlist don't have any dah element.

    0 讨论(0)
  • 2020-12-24 09:34

    probably these are a little bit simple:

    > z <- list(list(a=1, b=2), list(a=3, b=4))
    > sapply(z, `[[`, "b")
    [1] 2 4
    > sapply(z, get, x="b")
    [1] 2 4
    

    and you can define a function like:

    > `%c%` <- function(x, n)sapply(x, `[[`, n)
    > z %c% "b"
    [1] 2 4
    

    and also this looks like an extension of $:

    > `%$%` <- function(x, n) sapply(x, `[[`, as.character(as.list(match.call())$n))
    > z%$%b
    [1] 2 4
    
    0 讨论(0)
  • With Hadley's new lowliner package you can supply map() with a numeric index or an element name to elegantly pluck components out of a list. map() is the equivalent of lapply() with some extra tricks.

    library("lowliner")
    
    l <- list(
      list(a = 1, b = 2),
      list(a = 3, b = 4)
    )
    
    map(l, "b")
    map(l, 2)
    

    There is also a version that simplifies the result to a vector

    map_v(l, "a")
    map_v(l, 1)
    
    0 讨论(0)
提交回复
热议问题