replace {container} | R Documentation |
Replace Values in Containers Safely
Description
Try to find and replace elements and signal an error if not
found, unless it is stated to explicitly add the element (see option add
).
Usage
replace(.x, ...)
ref_replace(.x, ...)
## S3 method for class 'Container'
replace(.x, old, new, add = FALSE, ...)
## S3 method for class 'Container'
ref_replace(.x, old, new, add = FALSE, ...)
## S3 method for class 'Dict'
replace(.x, old, new, ...)
## S3 method for class 'Dict'
ref_replace(.x, old, new, ...)
Arguments
.x |
any |
... |
additional arguments to be passed to or from methods. |
old |
old element to be found and replaced. |
new |
the new element replacing the old one. |
add |
|
Details
replace
uses copy semantics while ref_replace
works by reference.
Value
For Container
, an object of class Container
(or one of the
respective derived classes).
For Dict
an object of class Dict
.
Examples
co = container("x", 9)
replace(co, 9, 0)
replace(co, "x", 0)
try({
replace(co, "z", 0) # old element ("z") is not in Container
})
replace(co, "z", 0, add = TRUE) # just add the zero without replacement
d = dict(a = 1, b = "z")
replace(d, 1, 1:5)
replace(d, "z", "a")
try({
replace(d, "a", 2) # old element ("a") is not in Dict
})
[Package container version 1.0.4 Index]