xpluck() provides an alternative to purrr::pluck(). Unlike purrr::pluck(), xpluck() allows you to extract multiple indices at each nesting level.

xpluck(.x, ..., .default = NULL)

Arguments

.x

A list or vector

...

A list of accessors for indexing into the object. Can be positive integers, negative integers (to index from the right), strings (to index into names) or missing (to keep all elements at a given level).

Unlike purrr::pluck(), each accessor may be a vector to extract multiple elements.

.default

Value to use if target is NULL or absent.

Value

A list or vector.

Examples

obj1 <- list("a", list(1, elt = "foo"))
obj2 <- list("b", list(2, elt = "bar"))
x <- list(obj1, obj2)

xpluck(x, 1:2, 2)
#> [[1]]
#> [[1]][[1]]
#> [1] 1
#> 
#> [[1]]$elt
#> [1] "foo"
#> 
#> 
#> [[2]]
#> [[2]][[1]]
#> [1] 2
#> 
#> [[2]]$elt
#> [1] "bar"
#> 
#> 
xpluck(x, , 2)
#> [[1]]
#> [[1]][[1]]
#> [1] 1
#> 
#> [[1]]$elt
#> [1] "foo"
#> 
#> 
#> [[2]]
#> [[2]][[1]]
#> [1] 2
#> 
#> [[2]]$elt
#> [1] "bar"
#> 
#> 

xpluck(x, , 2, 1)
#> [1] 1 2
xpluck(x, , 2, 2)
#> [1] "foo" "bar"
xpluck(x, , 2, 1:2)
#> [[1]]
#> [[1]][[1]]
#> [1] 1
#> 
#> [[1]][[2]]
#> [1] "foo"
#> 
#> 
#> [[2]]
#> [[2]][[1]]
#> [1] 2
#> 
#> [[2]][[2]]
#> [1] "bar"
#> 
#>