Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

list_modify

Modify a list


Description

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.

Usage

list_modify(.x, ...)

list_merge(.x, ...)

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)

# 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 = zap()))

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

purrr

Functional Programming Tools

v0.3.4
GPL-3 | file LICENSE
Authors
Lionel Henry [aut, cre], Hadley Wickham [aut], RStudio [cph, fnd]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.