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.

list_modify(.x, ...)

list_merge(.x, ...)

Arguments

.x

List to modify.

...

New values of a list. Use NULL to remove values. Use a formula to evaluate in the context of the list values. These dots have splicing semantics.

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 = NULL))
#> List of 2 #> $ x: int [1:10] 1 2 3 4 5 6 7 8 9 10 #> $ y: num 4
# 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 take dots with splicing. Use !!! or UQS() to # splice a list of arguments: l <- list(new = 1, y = NULL, 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
# In update_list() you can also use quosures and formulas to # compute new values. This function is likely to be deprecated in # the future str(update_list(x, z1 = ~z[[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 #> $ z1: num 1
str(update_list(x, z = rlang::quo(x + y)))
#> List of 3 #> $ x: int [1:10] 1 2 3 4 5 6 7 8 9 10 #> $ y: num 4 #> $ z: num [1:10] 5 6 7 8 9 10 11 12 13 14