Does a multi-value purrr::pluck exist?

前端 未结 4 790
既然无缘
既然无缘 2021-02-19 11:49

Seems like a basic question and perhaps I\'m just missing something obvious ... but is there any way to pluck a sublist (with purrr)? More specifically

4条回答
  •  你的背包
    2021-02-19 12:02

    Here's my take on it. Not based on purrr thus slightly off topic, but compatible with the tidyverse's general interface principle of playing nicely with pipes.

    pluck_multiple <- function(x, ...) {
      `[`(x, ...)
    }
    
    x <- list(a = 1, b = 2, c = list(x = TRUE, y = FALSE))
    
    pluck_multiple(x, c("a", "b"))
    #> $a
    #> [1] 1
    #> 
    #> $b
    #> [1] 2
    pluck_multiple(x, 2:3)
    #> $b
    #> [1] 2
    #> 
    #> $c
    #> $c$x
    #> [1] TRUE
    #> 
    #> $c$y
    #> [1] FALSE
    

提交回复
热议问题