Unlike map() and its variants which always return a fixed object type (list for map(), integer vector for map_int(), etc), the modify() family always returns the same type as the input object.

  • modify() is a shortcut for x[[i]] <- f(x[[i]]); return(x).

  • modify_if() only modifies the elements of x that satisfy a predicate and leaves the others unchanged. modify_at() only modifies elements given by names or positions.

  • modify2() modifies the elements of .x but also passes the elements of .y to .f, just like map2(). imodify() passes the names or the indices to .f like imap() does.

  • modify_depth() only modifies elements at a given level of a nested data structure.

  • modify_in() modifies a single element in a pluck() location.

modify(.x, .f, ...)

# S3 method for default
modify(.x, .f, ...)

modify_if(.x, .p, .f, ..., .else = NULL)

# S3 method for default
modify_if(.x, .p, .f, ..., .else = NULL)

modify_at(.x, .at, .f, ...)

# S3 method for default
modify_at(.x, .at, .f, ...)

modify2(.x, .y, .f, ...)

imodify(.x, .f, ...)

modify_depth(.x, .depth, .f, ..., .ragged = .depth < 0)

# S3 method for default
modify_depth(.x, .depth, .f, ..., .ragged = .depth < 0)

Arguments

.x

A list or atomic vector.

.f

A function, formula, or vector (not necessarily atomic).

If a function, it is used as is.

If a formula, e.g. ~ .x + 2, it is converted to a function. There are three ways to refer to the arguments:

  • For a single argument function, use .

  • For a two argument function, use .x and .y

  • For more arguments, use ..1, ..2, ..3 etc

This syntax allows you to create very compact anonymous functions.

If character vector, numeric vector, or list, it is converted to an extractor function. Character vectors index by name and numeric vectors index by position; use a list to index by position and name at different levels. If a component is not present, the value of .default will be returned.

...

Additional arguments passed on to the mapped function.

.p

A single predicate function, a formula describing such a predicate function, or a logical vector of the same length as .x. Alternatively, if the elements of .x are themselves lists of objects, a string indicating the name of a logical element in the inner lists. Only those elements where .p evaluates to TRUE will be modified.

.else

A function applied to elements of .x for which .p returns FALSE.

.at

A character vector of names, positive numeric vector of positions to include, or a negative numeric vector of positions to exlude. Only those elements corresponding to .at will be modified. If the tidyselect package is installed, you can use vars() and the tidyselect helpers to select elements.

.y

Vectors of the same length. A vector of length 1 will be recycled.

.depth

Level of .x to map on. Use a negative value to count up from the lowest level of the list.

  • modify_depth(x, 0, fun) is equivalent to x[] <- fun(x).

  • modify_depth(x, 1, fun) is equivalent to x <- modify(x, fun)

  • modify_depth(x, 2, fun) is equivalent to x <- modify(x, ~ modify(., fun))

.ragged

If TRUE, will apply to leaves, even if they're not at depth .depth. If FALSE, will throw an error if there are no elements at depth .depth.

Value

An object the same class as .x

Details

Since the transformation can alter the structure of the input; it's your responsibility to ensure that the transformation produces a valid output. For example, if you're modifying a data frame, .f must preserve the length of the input.

Genericity

modify() and variants are generic over classes that implement length(), [[ and [[<- methods. If the default implementation is not compatible for your class, you can override them with your own methods.

If you implement your own modify() method, make sure it satisfies the following invariants:

modify(x, identity) === x
modify(x, compose(f, g)) === modify(x, g) %&gt;% modify(f)

These invariants are known as the functor laws in computer science.

See also

Other map variants: imap(), invoke(), lmap(), map2(), map_if(), map()

Examples

# Convert factors to characters iris %>% modify_if(is.factor, as.character) %>% str()
#> 'data.frame': 150 obs. of 5 variables: #> $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... #> $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... #> $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... #> $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ... #> $ Species : chr "setosa" "setosa" "setosa" "setosa" ...
# Specify which columns to map with a numeric vector of positions: mtcars %>% modify_at(c(1, 4, 5), as.character) %>% str()
#> 'data.frame': 32 obs. of 11 variables: #> $ mpg : chr "21" "21" "22.8" "21.4" ... #> $ cyl : num 6 6 4 6 8 6 8 4 4 6 ... #> $ disp: num 160 160 108 258 360 ... #> $ hp : chr "110" "110" "93" "110" ... #> $ drat: chr "3.9" "3.9" "3.85" "3.08" ... #> $ wt : num 2.62 2.88 2.32 3.21 3.44 ... #> $ qsec: num 16.5 17 18.6 19.4 17 ... #> $ vs : num 0 0 1 1 0 1 0 1 1 1 ... #> $ am : num 1 1 1 0 0 0 0 0 0 0 ... #> $ gear: num 4 4 4 3 3 3 3 4 4 4 ... #> $ carb: num 4 4 1 1 2 1 4 2 2 4 ...
# Or with a vector of names: mtcars %>% modify_at(c("cyl", "am"), as.character) %>% str()
#> 'data.frame': 32 obs. of 11 variables: #> $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ... #> $ cyl : chr "6" "6" "4" "6" ... #> $ disp: num 160 160 108 258 360 ... #> $ hp : num 110 110 93 110 175 105 245 62 95 123 ... #> $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ... #> $ wt : num 2.62 2.88 2.32 3.21 3.44 ... #> $ qsec: num 16.5 17 18.6 19.4 17 ... #> $ vs : num 0 0 1 1 0 1 0 1 1 1 ... #> $ am : chr "1" "1" "1" "0" ... #> $ gear: num 4 4 4 3 3 3 3 4 4 4 ... #> $ carb: num 4 4 1 1 2 1 4 2 2 4 ...
list(x = rbernoulli(100), y = 1:100) %>% transpose() %>% modify_if("x", ~ update_list(., y = ~ y * 100)) %>% transpose() %>% simplify_all()
#> $x #> [1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [13] FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE #> [25] FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE TRUE TRUE FALSE FALSE #> [37] FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE #> [49] FALSE TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE #> [61] TRUE TRUE FALSE FALSE TRUE TRUE TRUE FALSE TRUE FALSE TRUE FALSE #> [73] TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE #> [85] FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE TRUE TRUE TRUE FALSE #> [97] TRUE TRUE FALSE FALSE #> #> $y #> [1] 1 2 3 4 500 6 7 8 9 10 11 12 13 14 15 #> [16] 1600 17 18 19 2000 21 22 23 24 25 2600 27 2800 29 3000 #> [31] 31 32 3300 3400 35 36 37 3800 3900 4000 41 42 43 44 45 #> [46] 46 4700 4800 49 5000 51 5200 53 54 5500 5600 5700 5800 59 60 #> [61] 6100 6200 63 64 6500 6600 6700 68 6900 70 7100 72 7300 7400 7500 #> [76] 7600 7700 78 79 80 81 8200 8300 84 85 8600 87 8800 8900 90 #> [91] 91 92 9300 9400 9500 96 9700 9800 99 100 #>
# Use modify2() to map over two vectors and preserve the type of # the first one: x <- c(foo = 1L, bar = 2L) y <- c(TRUE, FALSE) modify2(x, y, ~ if (.y) .x else 0L)
#> foo bar #> 1 0
# Use a predicate function to decide whether to map a function: modify_if(iris, is.factor, as.character)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1 5.1 3.5 1.4 0.2 setosa #> 2 4.9 3.0 1.4 0.2 setosa #> 3 4.7 3.2 1.3 0.2 setosa #> 4 4.6 3.1 1.5 0.2 setosa #> 5 5.0 3.6 1.4 0.2 setosa #> 6 5.4 3.9 1.7 0.4 setosa #> 7 4.6 3.4 1.4 0.3 setosa #> 8 5.0 3.4 1.5 0.2 setosa #> 9 4.4 2.9 1.4 0.2 setosa #> 10 4.9 3.1 1.5 0.1 setosa #> 11 5.4 3.7 1.5 0.2 setosa #> 12 4.8 3.4 1.6 0.2 setosa #> 13 4.8 3.0 1.4 0.1 setosa #> 14 4.3 3.0 1.1 0.1 setosa #> 15 5.8 4.0 1.2 0.2 setosa #> 16 5.7 4.4 1.5 0.4 setosa #> 17 5.4 3.9 1.3 0.4 setosa #> 18 5.1 3.5 1.4 0.3 setosa #> 19 5.7 3.8 1.7 0.3 setosa #> 20 5.1 3.8 1.5 0.3 setosa #> 21 5.4 3.4 1.7 0.2 setosa #> 22 5.1 3.7 1.5 0.4 setosa #> 23 4.6 3.6 1.0 0.2 setosa #> 24 5.1 3.3 1.7 0.5 setosa #> 25 4.8 3.4 1.9 0.2 setosa #> 26 5.0 3.0 1.6 0.2 setosa #> 27 5.0 3.4 1.6 0.4 setosa #> 28 5.2 3.5 1.5 0.2 setosa #> 29 5.2 3.4 1.4 0.2 setosa #> 30 4.7 3.2 1.6 0.2 setosa #> 31 4.8 3.1 1.6 0.2 setosa #> 32 5.4 3.4 1.5 0.4 setosa #> 33 5.2 4.1 1.5 0.1 setosa #> 34 5.5 4.2 1.4 0.2 setosa #> 35 4.9 3.1 1.5 0.2 setosa #> 36 5.0 3.2 1.2 0.2 setosa #> 37 5.5 3.5 1.3 0.2 setosa #> 38 4.9 3.6 1.4 0.1 setosa #> 39 4.4 3.0 1.3 0.2 setosa #> 40 5.1 3.4 1.5 0.2 setosa #> 41 5.0 3.5 1.3 0.3 setosa #> 42 4.5 2.3 1.3 0.3 setosa #> 43 4.4 3.2 1.3 0.2 setosa #> 44 5.0 3.5 1.6 0.6 setosa #> 45 5.1 3.8 1.9 0.4 setosa #> 46 4.8 3.0 1.4 0.3 setosa #> 47 5.1 3.8 1.6 0.2 setosa #> 48 4.6 3.2 1.4 0.2 setosa #> 49 5.3 3.7 1.5 0.2 setosa #> 50 5.0 3.3 1.4 0.2 setosa #> 51 7.0 3.2 4.7 1.4 versicolor #> 52 6.4 3.2 4.5 1.5 versicolor #> 53 6.9 3.1 4.9 1.5 versicolor #> 54 5.5 2.3 4.0 1.3 versicolor #> 55 6.5 2.8 4.6 1.5 versicolor #> 56 5.7 2.8 4.5 1.3 versicolor #> 57 6.3 3.3 4.7 1.6 versicolor #> 58 4.9 2.4 3.3 1.0 versicolor #> 59 6.6 2.9 4.6 1.3 versicolor #> 60 5.2 2.7 3.9 1.4 versicolor #> 61 5.0 2.0 3.5 1.0 versicolor #> 62 5.9 3.0 4.2 1.5 versicolor #> 63 6.0 2.2 4.0 1.0 versicolor #> 64 6.1 2.9 4.7 1.4 versicolor #> 65 5.6 2.9 3.6 1.3 versicolor #> 66 6.7 3.1 4.4 1.4 versicolor #> 67 5.6 3.0 4.5 1.5 versicolor #> 68 5.8 2.7 4.1 1.0 versicolor #> 69 6.2 2.2 4.5 1.5 versicolor #> 70 5.6 2.5 3.9 1.1 versicolor #> 71 5.9 3.2 4.8 1.8 versicolor #> 72 6.1 2.8 4.0 1.3 versicolor #> 73 6.3 2.5 4.9 1.5 versicolor #> 74 6.1 2.8 4.7 1.2 versicolor #> 75 6.4 2.9 4.3 1.3 versicolor #> 76 6.6 3.0 4.4 1.4 versicolor #> 77 6.8 2.8 4.8 1.4 versicolor #> 78 6.7 3.0 5.0 1.7 versicolor #> 79 6.0 2.9 4.5 1.5 versicolor #> 80 5.7 2.6 3.5 1.0 versicolor #> 81 5.5 2.4 3.8 1.1 versicolor #> 82 5.5 2.4 3.7 1.0 versicolor #> 83 5.8 2.7 3.9 1.2 versicolor #> 84 6.0 2.7 5.1 1.6 versicolor #> 85 5.4 3.0 4.5 1.5 versicolor #> 86 6.0 3.4 4.5 1.6 versicolor #> 87 6.7 3.1 4.7 1.5 versicolor #> 88 6.3 2.3 4.4 1.3 versicolor #> 89 5.6 3.0 4.1 1.3 versicolor #> 90 5.5 2.5 4.0 1.3 versicolor #> 91 5.5 2.6 4.4 1.2 versicolor #> 92 6.1 3.0 4.6 1.4 versicolor #> 93 5.8 2.6 4.0 1.2 versicolor #> 94 5.0 2.3 3.3 1.0 versicolor #> 95 5.6 2.7 4.2 1.3 versicolor #> 96 5.7 3.0 4.2 1.2 versicolor #> 97 5.7 2.9 4.2 1.3 versicolor #> 98 6.2 2.9 4.3 1.3 versicolor #> 99 5.1 2.5 3.0 1.1 versicolor #> 100 5.7 2.8 4.1 1.3 versicolor #> 101 6.3 3.3 6.0 2.5 virginica #> 102 5.8 2.7 5.1 1.9 virginica #> 103 7.1 3.0 5.9 2.1 virginica #> 104 6.3 2.9 5.6 1.8 virginica #> 105 6.5 3.0 5.8 2.2 virginica #> 106 7.6 3.0 6.6 2.1 virginica #> 107 4.9 2.5 4.5 1.7 virginica #> 108 7.3 2.9 6.3 1.8 virginica #> 109 6.7 2.5 5.8 1.8 virginica #> 110 7.2 3.6 6.1 2.5 virginica #> 111 6.5 3.2 5.1 2.0 virginica #> 112 6.4 2.7 5.3 1.9 virginica #> 113 6.8 3.0 5.5 2.1 virginica #> 114 5.7 2.5 5.0 2.0 virginica #> 115 5.8 2.8 5.1 2.4 virginica #> 116 6.4 3.2 5.3 2.3 virginica #> 117 6.5 3.0 5.5 1.8 virginica #> 118 7.7 3.8 6.7 2.2 virginica #> 119 7.7 2.6 6.9 2.3 virginica #> 120 6.0 2.2 5.0 1.5 virginica #> 121 6.9 3.2 5.7 2.3 virginica #> 122 5.6 2.8 4.9 2.0 virginica #> 123 7.7 2.8 6.7 2.0 virginica #> 124 6.3 2.7 4.9 1.8 virginica #> 125 6.7 3.3 5.7 2.1 virginica #> 126 7.2 3.2 6.0 1.8 virginica #> 127 6.2 2.8 4.8 1.8 virginica #> 128 6.1 3.0 4.9 1.8 virginica #> 129 6.4 2.8 5.6 2.1 virginica #> 130 7.2 3.0 5.8 1.6 virginica #> 131 7.4 2.8 6.1 1.9 virginica #> 132 7.9 3.8 6.4 2.0 virginica #> 133 6.4 2.8 5.6 2.2 virginica #> 134 6.3 2.8 5.1 1.5 virginica #> 135 6.1 2.6 5.6 1.4 virginica #> 136 7.7 3.0 6.1 2.3 virginica #> 137 6.3 3.4 5.6 2.4 virginica #> 138 6.4 3.1 5.5 1.8 virginica #> 139 6.0 3.0 4.8 1.8 virginica #> 140 6.9 3.1 5.4 2.1 virginica #> 141 6.7 3.1 5.6 2.4 virginica #> 142 6.9 3.1 5.1 2.3 virginica #> 143 5.8 2.7 5.1 1.9 virginica #> 144 6.8 3.2 5.9 2.3 virginica #> 145 6.7 3.3 5.7 2.5 virginica #> 146 6.7 3.0 5.2 2.3 virginica #> 147 6.3 2.5 5.0 1.9 virginica #> 148 6.5 3.0 5.2 2.0 virginica #> 149 6.2 3.4 5.4 2.3 virginica #> 150 5.9 3.0 5.1 1.8 virginica
# Specify an alternative with the `.else` argument: modify_if(iris, is.factor, as.character, .else = as.integer)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1 5 3 1 0 setosa #> 2 4 3 1 0 setosa #> 3 4 3 1 0 setosa #> 4 4 3 1 0 setosa #> 5 5 3 1 0 setosa #> 6 5 3 1 0 setosa #> 7 4 3 1 0 setosa #> 8 5 3 1 0 setosa #> 9 4 2 1 0 setosa #> 10 4 3 1 0 setosa #> 11 5 3 1 0 setosa #> 12 4 3 1 0 setosa #> 13 4 3 1 0 setosa #> 14 4 3 1 0 setosa #> 15 5 4 1 0 setosa #> 16 5 4 1 0 setosa #> 17 5 3 1 0 setosa #> 18 5 3 1 0 setosa #> 19 5 3 1 0 setosa #> 20 5 3 1 0 setosa #> 21 5 3 1 0 setosa #> 22 5 3 1 0 setosa #> 23 4 3 1 0 setosa #> 24 5 3 1 0 setosa #> 25 4 3 1 0 setosa #> 26 5 3 1 0 setosa #> 27 5 3 1 0 setosa #> 28 5 3 1 0 setosa #> 29 5 3 1 0 setosa #> 30 4 3 1 0 setosa #> 31 4 3 1 0 setosa #> 32 5 3 1 0 setosa #> 33 5 4 1 0 setosa #> 34 5 4 1 0 setosa #> 35 4 3 1 0 setosa #> 36 5 3 1 0 setosa #> 37 5 3 1 0 setosa #> 38 4 3 1 0 setosa #> 39 4 3 1 0 setosa #> 40 5 3 1 0 setosa #> 41 5 3 1 0 setosa #> 42 4 2 1 0 setosa #> 43 4 3 1 0 setosa #> 44 5 3 1 0 setosa #> 45 5 3 1 0 setosa #> 46 4 3 1 0 setosa #> 47 5 3 1 0 setosa #> 48 4 3 1 0 setosa #> 49 5 3 1 0 setosa #> 50 5 3 1 0 setosa #> 51 7 3 4 1 versicolor #> 52 6 3 4 1 versicolor #> 53 6 3 4 1 versicolor #> 54 5 2 4 1 versicolor #> 55 6 2 4 1 versicolor #> 56 5 2 4 1 versicolor #> 57 6 3 4 1 versicolor #> 58 4 2 3 1 versicolor #> 59 6 2 4 1 versicolor #> 60 5 2 3 1 versicolor #> 61 5 2 3 1 versicolor #> 62 5 3 4 1 versicolor #> 63 6 2 4 1 versicolor #> 64 6 2 4 1 versicolor #> 65 5 2 3 1 versicolor #> 66 6 3 4 1 versicolor #> 67 5 3 4 1 versicolor #> 68 5 2 4 1 versicolor #> 69 6 2 4 1 versicolor #> 70 5 2 3 1 versicolor #> 71 5 3 4 1 versicolor #> 72 6 2 4 1 versicolor #> 73 6 2 4 1 versicolor #> 74 6 2 4 1 versicolor #> 75 6 2 4 1 versicolor #> 76 6 3 4 1 versicolor #> 77 6 2 4 1 versicolor #> 78 6 3 5 1 versicolor #> 79 6 2 4 1 versicolor #> 80 5 2 3 1 versicolor #> 81 5 2 3 1 versicolor #> 82 5 2 3 1 versicolor #> 83 5 2 3 1 versicolor #> 84 6 2 5 1 versicolor #> 85 5 3 4 1 versicolor #> 86 6 3 4 1 versicolor #> 87 6 3 4 1 versicolor #> 88 6 2 4 1 versicolor #> 89 5 3 4 1 versicolor #> 90 5 2 4 1 versicolor #> 91 5 2 4 1 versicolor #> 92 6 3 4 1 versicolor #> 93 5 2 4 1 versicolor #> 94 5 2 3 1 versicolor #> 95 5 2 4 1 versicolor #> 96 5 3 4 1 versicolor #> 97 5 2 4 1 versicolor #> 98 6 2 4 1 versicolor #> 99 5 2 3 1 versicolor #> 100 5 2 4 1 versicolor #> 101 6 3 6 2 virginica #> 102 5 2 5 1 virginica #> 103 7 3 5 2 virginica #> 104 6 2 5 1 virginica #> 105 6 3 5 2 virginica #> 106 7 3 6 2 virginica #> 107 4 2 4 1 virginica #> 108 7 2 6 1 virginica #> 109 6 2 5 1 virginica #> 110 7 3 6 2 virginica #> 111 6 3 5 2 virginica #> 112 6 2 5 1 virginica #> 113 6 3 5 2 virginica #> 114 5 2 5 2 virginica #> 115 5 2 5 2 virginica #> 116 6 3 5 2 virginica #> 117 6 3 5 1 virginica #> 118 7 3 6 2 virginica #> 119 7 2 6 2 virginica #> 120 6 2 5 1 virginica #> 121 6 3 5 2 virginica #> 122 5 2 4 2 virginica #> 123 7 2 6 2 virginica #> 124 6 2 4 1 virginica #> 125 6 3 5 2 virginica #> 126 7 3 6 1 virginica #> 127 6 2 4 1 virginica #> 128 6 3 4 1 virginica #> 129 6 2 5 2 virginica #> 130 7 3 5 1 virginica #> 131 7 2 6 1 virginica #> 132 7 3 6 2 virginica #> 133 6 2 5 2 virginica #> 134 6 2 5 1 virginica #> 135 6 2 5 1 virginica #> 136 7 3 6 2 virginica #> 137 6 3 5 2 virginica #> 138 6 3 5 1 virginica #> 139 6 3 4 1 virginica #> 140 6 3 5 2 virginica #> 141 6 3 5 2 virginica #> 142 6 3 5 2 virginica #> 143 5 2 5 1 virginica #> 144 6 3 5 2 virginica #> 145 6 3 5 2 virginica #> 146 6 3 5 2 virginica #> 147 6 2 5 1 virginica #> 148 6 3 5 2 virginica #> 149 6 3 5 2 virginica #> 150 5 3 5 1 virginica
# Modify at specified depth --------------------------- l1 <- list( obj1 = list( prop1 = list(param1 = 1:2, param2 = 3:4), prop2 = list(param1 = 5:6, param2 = 7:8) ), obj2 = list( prop1 = list(param1 = 9:10, param2 = 11:12), prop2 = list(param1 = 12:14, param2 = 15:17) ) ) # In the above list, "obj" is level 1, "prop" is level 2 and "param" # is level 3. To apply sum() on all params, we map it at depth 3: l1 %>% modify_depth(3, sum) %>% str()
#> List of 2 #> $ obj1:List of 2 #> ..$ prop1:List of 2 #> .. ..$ param1: int 3 #> .. ..$ param2: int 7 #> ..$ prop2:List of 2 #> .. ..$ param1: int 11 #> .. ..$ param2: int 15 #> $ obj2:List of 2 #> ..$ prop1:List of 2 #> .. ..$ param1: int 19 #> .. ..$ param2: int 23 #> ..$ prop2:List of 2 #> .. ..$ param1: int 39 #> .. ..$ param2: int 48
# Note that vectorised operations will yield the same result when # applied at the list level as when applied at the atomic result. # The former is more efficient because it takes advantage of # vectorisation. l1 %>% modify_depth(3, `+`, 100L)
#> $obj1 #> $obj1$prop1 #> $obj1$prop1$param1 #> [1] 101 102 #> #> $obj1$prop1$param2 #> [1] 103 104 #> #> #> $obj1$prop2 #> $obj1$prop2$param1 #> [1] 105 106 #> #> $obj1$prop2$param2 #> [1] 107 108 #> #> #> #> $obj2 #> $obj2$prop1 #> $obj2$prop1$param1 #> [1] 109 110 #> #> $obj2$prop1$param2 #> [1] 111 112 #> #> #> $obj2$prop2 #> $obj2$prop2$param1 #> [1] 112 113 114 #> #> $obj2$prop2$param2 #> [1] 115 116 117 #> #> #>
l1 %>% modify_depth(4, `+`, 100L)
#> $obj1 #> $obj1$prop1 #> $obj1$prop1$param1 #> [1] 101 102 #> #> $obj1$prop1$param2 #> [1] 103 104 #> #> #> $obj1$prop2 #> $obj1$prop2$param1 #> [1] 105 106 #> #> $obj1$prop2$param2 #> [1] 107 108 #> #> #> #> $obj2 #> $obj2$prop1 #> $obj2$prop1$param1 #> [1] 109 110 #> #> $obj2$prop1$param2 #> [1] 111 112 #> #> #> $obj2$prop2 #> $obj2$prop2$param1 #> [1] 112 113 114 #> #> $obj2$prop2$param2 #> [1] 115 116 117 #> #> #>
# modify() lets us pluck the elements prop1/param2 in obj1 and obj2: l1 %>% modify(c("prop1", "param2")) %>% str()
#> List of 2 #> $ obj1: int [1:2] 3 4 #> $ obj2: int [1:2] 11 12
# But what if we want to pluck all param2 elements? Then we need to # act at a lower level: l1 %>% modify_depth(2, "param2") %>% str()
#> List of 2 #> $ obj1:List of 2 #> ..$ prop1: int [1:2] 3 4 #> ..$ prop2: int [1:2] 7 8 #> $ obj2:List of 2 #> ..$ prop1: int [1:2] 11 12 #> ..$ prop2: int [1:3] 15 16 17
# modify_depth() can be with other purrr functions to make them operate at # a lower level. Here we ask pmap() to map paste() simultaneously over all # elements of the objects at the second level. paste() is effectively # mapped at level 3. l1 %>% modify_depth(2, ~ pmap(., paste, sep = " / ")) %>% str()
#> List of 2 #> $ obj1:List of 2 #> ..$ prop1:List of 2 #> .. ..$ : chr "1 / 3" #> .. ..$ : chr "2 / 4" #> ..$ prop2:List of 2 #> .. ..$ : chr "5 / 7" #> .. ..$ : chr "6 / 8" #> $ obj2:List of 2 #> ..$ prop1:List of 2 #> .. ..$ : chr "9 / 11" #> .. ..$ : chr "10 / 12" #> ..$ prop2:List of 3 #> .. ..$ : chr "12 / 15" #> .. ..$ : chr "13 / 16" #> .. ..$ : chr "14 / 17"