Replace parts of a Container
object similar
to R's base replace operators on lists.
# S3 method for Container
[(x, i) <- value
# S3 method for Container
[[(x, i) <- value
# S3 method for Container
$(x, name) <- value
Container
object in which to replace elements.
indices specifying elements to replace. Indices
are numeric
or character
vectors or a list
containing both.
the replacing value of ANY
type
character
string (possibly backtick quoted)
[<-
replaces multiple values. The indices can be numeric
or
character
or both. They can be passed as a vector
or list
. Values can
be added by 'replacing' at new indices, which only works for character
indices.
[[<-
replaces a single value at a given numeric
or character
index.
Instead of an index, it is also possible to replace certain elements by
passing the element in curly braces (see Examples), that is, the object is
searched for the element and then the element is replaced by the value.
$<-
replaces a single element at a given name.
co = container(a = 1, b = "bar")
(co[1:2] <- 1:2)
#> [1] 1 2
try({
co[3] <- 3 # index out of range
})
#> Error : index out of range (length = 2): 3
(co[list(1, "b")] <- 3:4) # mixed numeric/character index
#> [1] 3 4
co = container(a = 1, b = 2)
co[[1]] <- 9
co[["b"]] <- 8
co[["x"]] <- 7
co$z <- 99
print(co)
#> [a = 9, b = 8, x = 7, z = 99]
# Replace 8 by 0
co[[{8}]] <- 0
print(co)
#> [a = 9, b = 0, x = 7, z = 99]
co = container(a = 1, b = "bar")
co$f <- 3
co$b <- 2
co
#> [a = 1, b = 2, f = 3]