list_modify()
and list_merge()
recursively combine two lists, matching
elements either by name or position. If a sub-element is present in
both lists list_modify()
takes the value from y
, and list_merge()
concatenates the values together.
update_list()
handles formulas and quosures that can refer to
values existing within the input list. Note that this function
might be deprecated in the future in favour of a dplyr::mutate()
method for lists.
Arguments
- .x
List to modify.
- ...
New values of a list. Use
zap()
to remove values.These values should be either all named or all unnamed. When inputs are all named, they are matched to
.x
by name. When they are all unnamed, they are matched positionally.These dots support tidy dots features. In particular, if your functions are stored in a list, you can splice that in with
!!!
.
Examples
x <- list(x = 1:10, y = 4, z = list(a = 1, b = 2))
str(x)
#> List of 3
#> $ x: int [1:10] 1 2 3 4 5 6 7 8 9 10
#> $ y: num 4
#> $ z:List of 2
#> ..$ a: num 1
#> ..$ b: num 2
# Update values
str(list_modify(x, a = 1))
#> List of 4
#> $ x: int [1:10] 1 2 3 4 5 6 7 8 9 10
#> $ y: num 4
#> $ z:List of 2
#> ..$ a: num 1
#> ..$ b: num 2
#> $ a: num 1
# Replace values
str(list_modify(x, z = 5))
#> List of 3
#> $ x: int [1:10] 1 2 3 4 5 6 7 8 9 10
#> $ y: num 4
#> $ z: num 5
str(list_modify(x, z = list(a = 1:5)))
#> List of 3
#> $ x: int [1:10] 1 2 3 4 5 6 7 8 9 10
#> $ y: num 4
#> $ z:List of 2
#> ..$ a: int [1:5] 1 2 3 4 5
#> ..$ b: num 2
# Remove values
str(list_modify(x, z = zap()))
#> List of 3
#> $ x: int [1:10] 1 2 3 4 5 6 7 8 9 10
#> $ y: num 4
#> $ z:List of 2
#> ..$ a: num 1
#> ..$ b: num 2
# Combine values
str(list_merge(x, x = 11, z = list(a = 2:5, c = 3)))
#> List of 3
#> $ x: num [1:11] 1 2 3 4 5 6 7 8 9 10 ...
#> $ y: num 4
#> $ z:List of 3
#> ..$ a: num [1:5] 1 2 3 4 5
#> ..$ b: num 2
#> ..$ c: num 3
# All these functions support tidy dots features. Use !!! to splice
# a list of arguments:
l <- list(new = 1, y = zap(), z = 5)
str(list_modify(x, !!!l))
#> List of 3
#> $ x : int [1:10] 1 2 3 4 5 6 7 8 9 10
#> $ z : num 5
#> $ new: num 1