Learn R Programming

purrr (version 0.2.3)

list_modify: Modify a list

Description

list_modify() and list_merge() recursively combine two lists, matching elements either by name or position. If an 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.

Usage

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

Run this code
# NOT RUN {
x <- list(x = 1:10, y = 4, z = list(a = 1, b = 2))
str(x)

# Update values
str(list_modify(x, a = 1))
# Replace values
str(list_modify(x, z = 5))
str(list_modify(x, z = list(a = 1:5)))
# Remove values
str(list_modify(x, z = NULL))

# Combine values
str(list_merge(x, x = 11, z = list(a = 2:5, c = 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))

# In update_list() you can also use quosures and formulas to
# compute new values. This function is likely to be deprecated in
# the future
update_list(x, z1 = ~z[[1]])
update_list(x, z = rlang::quo(x + y))
# }

Run the code above in your browser using DataLab