modify_in: Modify a pluck location

Description Usage Arguments See Also Examples

View source: R/modify.R

Description

The pluck location must exist.

Usage

1
2
3
modify_in(.x, .where, .f, ...)

assign_in(x, where, value)

Arguments

.x

A vector or environment

.where, where

A pluck location, as a numeric vector of positions, a character vector of names, or a list combining both. The location must exist in the data structure.

.f

A function to apply at the pluck location given by .where.

...

Arguments passed to .f.

x

A vector or environment

value

A value to replace in .x at the pluck location.

See Also

pluck()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Recall that pluck() returns a component of a data structure that
# might be arbitrarily deep
x <- list(list(bar = 1, foo = 2))
pluck(x, 1, "foo")

# Use assign_in() to modify the pluck location:
assign_in(x, list(1, "foo"), 100)

# modify_in() applies a function to that location and update the
# element in place:
modify_in(x, list(1, "foo"), ~ .x * 200)

# Additional arguments are passed to the function in the ordinary way:
modify_in(x, list(1, "foo"), `+`, 100)

Example output

[1] 2
[[1]]
[[1]]$bar
[1] 1

[[1]]$foo
[1] 100


[[1]]
[[1]]$bar
[1] 1

[[1]]$foo
[1] 400


[[1]]
[[1]]$bar
[1] 1

[[1]]$foo
[1] 102

purrr documentation built on April 19, 2020, 4:17 p.m.