xpluck()
provides an alternative to purrr::pluck()
.
Unlike purrr::pluck()
, xpluck()
allows you to extract multiple indices at
each nesting level.
Arguments
- .x
- ...
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.If an accessor has length 0 (e.g.
NULL
,character(0)
ornumeric(0)
),xpluck()
will returnNULL
.- .default
Value to use if target is
NULL
or absent.
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"
#>
#>