Delete a nested element by path
Arguments
- x
A nested list to traverse.
Lists are traversed recursively. Non-list objects are treated as leaves. Data frames are not traversed and are treated as leaf values.
- path
Character or integer vector giving the path to retrieve.
For a character path, each component selects a named element at the current level. For an integer path, each component selects an element by position. Positional indices are one-based, following normal R indexing conventions.
The path is evaluated from left to right. For example,
c("b", "y", "i")meansx[["b"]][["y"]][["i"]].
See also
list_get(), list_has(), list_set()
Other lstrrr_core:
list_depth(),
list_get(),
list_has(),
list_nleaf(),
list_path(),
list_set()
Examples
x <- list(
a = 1,
b = list(
x = 1,
y = list(i = 1, j = 2)
)
)
list_delete(x, c("b", "y", "j"))
#> $a
#> [1] 1
#>
#> $b
#> $b$x
#> [1] 1
#>
#> $b$y
#> $b$y$i
#> [1] 1
#>
#>
#>
# Delete by integer path
list_delete(x, c(2L, 2L, 1L))
#> $a
#> [1] 1
#>
#> $b
#> $b$x
#> [1] 1
#>
#> $b$y
#> $b$y$j
#> [1] 2
#>
#>
#>
# The original object is unchanged unless reassigned
x2 <- list_delete(x, c("b", "x"))
names(x$b)
#> [1] "x" "y"
names(x2$b)
#> [1] "y"
