How to select elements with the same name from nested list with purrr?

前端 未结 3 1000
轮回少年
轮回少年 2021-02-09 23:50
require(purrr)

list <- list( 
  node = list(a = list(y = 1, t = 1), b = list(y = 1, t = 2)),
  node = list(a = list(y = 1, t = 3), b = list(y = 1, t = 4))) 
<         


        
3条回答
  •  走了就别回头了
    2021-02-10 00:30

    With purrr::map

    map(list, ~map(.x, ~.x$t))
    

    Output is still list of list

    $node
    $node$a
    [1] 1
    
    $node$b
    [1] 2
    
    
    $node
    $node$a
    [1] 3
    
    $node$b
    [1] 4
    
    unlist
    

    To convert to vector

    unlist(map(list, ~map(.x, ~.x$t)))
    

    Output

    node.a node.b node.a node.b 
         1      2      3      4     
    

提交回复
热议问题