Try to find and replace elements at given indices and signal an
error if not found, unless it is stated to explicitly add the element (see
option add
).
Usage
replace_at(.x, ...)
ref_replace_at(.x, ...)
# S3 method for class 'Container'
replace_at(.x, ..., .add = FALSE)
# S3 method for class 'Container'
ref_replace_at(.x, ..., .add = FALSE)
# S3 method for class 'dict.table'
replace_at(.x, ..., .add = FALSE)
# S3 method for class 'dict.table'
ref_replace_at(.x, ..., .add = FALSE)
Arguments
- .x
any
R
object.- ...
either name = value pairs or two vectors/lists with names/values to be replaced.
- .add
logical
ifFALSE
(default) and index is invalid, an error is given. If set toTRUE
the new element is added at the given index regardless whether the index existed or not. Indices can consist of numbers or names or both, except when adding values at new indices, which is only allowed for names.
Value
For Container
, an object of class Container
(or one of the
respective derived classes).
For dict.table
an object of class dict.table
.
Examples
co = container(a = 0, b = "z")
replace_at(co, a = 1, b = 2)
#> [a = 1, b = 2]
replace_at(co, 1:2, 1:2) # same
#> [a = 1L, b = 2L]
replace_at(co, c("a", "b"), list(1, 2)) # same
#> [a = 1, b = 2]
try({
replace_at(co, x = 1) # names(s) not found: 'x'
})
#> Error : names(s) not found: 'x'
replace_at(co, x = 1, .add = TRUE) # ok (adds x = 1)
#> [a = 0, b = "z", x = 1]
dit = dict.table(a = 1:3, b = 4:6)
replace_at(dit, a = 3:1)
#> <dict.table> with 3 rows and 2 columns
#> a b
#> <int> <int>
#> 1: 3 4
#> 2: 2 5
#> 3: 1 6
replace_at(dit, 1, 3:1) # same
#> <dict.table> with 3 rows and 2 columns
#> a b
#> <int> <int>
#> 1: 3 4
#> 2: 2 5
#> 3: 1 6
replace_at(dit, "a", 3:1) # same
#> <dict.table> with 3 rows and 2 columns
#> a b
#> <int> <int>
#> 1: 3 4
#> 2: 2 5
#> 3: 1 6
replace_at(dit, a = 3:1, b = 6:4)
#> <dict.table> with 3 rows and 2 columns
#> a b
#> <int> <int>
#> 1: 3 6
#> 2: 2 5
#> 3: 1 4
replace_at(dit, 1:2, list(3:1, 6:4)) # same
#> <dict.table> with 3 rows and 2 columns
#> a b
#> <int> <int>
#> 1: 3 6
#> 2: 2 5
#> 3: 1 4
try({
replace_at(dit, x = 1) # column(s) not found: 'x'
})
#> Error : column(s) not found: 'x'
replace_at(dit, x = 1, .add = TRUE) # ok (adds column)
#> <dict.table> with 3 rows and 3 columns
#> a b x
#> <int> <int> <num>
#> 1: 1 4 1
#> 2: 2 5 1
#> 3: 3 6 1