replace | R Documentation |
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
).
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, ...)
.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 |
|
replace
uses copy semantics while ref_replace
works by reference.
For Container
, an object of class Container
(or one of the
respective derived classes).
For Dict
an object of class Dict
.
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 })
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.