| slab {dembase} | R Documentation |
Extract a slab length(elements) thick from object,
perpendicular to the dimension specified by dimension, or replace the
current values of the slab with new values
slab(object, dimension, elements, drop = TRUE) slab(object, dimension, elements, drop = TRUE) <- value ## S4 method for signature 'Component' slab(object, dimension, elements, drop = TRUE) ## S4 method for signature 'Births' slab(object, dimension, elements, drop = TRUE) ## S4 method for signature 'InternalMovementsPool' slab(object, dimension, elements, drop = TRUE) ## S4 method for signature 'InternalMovementsNet' slab(object, dimension, elements, drop = TRUE) ## S4 method for signature 'DemographicArray' slab(object, dimension, elements, drop = TRUE) ## S4 replacement method for signature 'DemographicArray' slab(object, dimension, elements, drop = TRUE) <- value
object |
Object of class |
dimension |
Name or index of dimension. |
elements |
Labels or indices of elements with in |
drop |
|
value |
An numeric vector, or an object that can be coerced to a numeric vector. |
slab is designed for programming, in situations where the dimension
of an object is not fixed in advance (which makes [ inconvenient).
In interactive use, subarray may be a better choice, as
it is clearer and more flexible.
When drop is set to TRUE or FALSE, it works exactly
like the drop argument in standard R extract functions
(eg Extract. If drop is TRUE, then
all length-one dimensions in the return value are removed; if
drop is FALSE, then all length-one dimensions are preserved.
slab, however, has a third option, the word "dimension".
In this case the only dimension eligible for dropping if it is
length one is the dimension specified by the dimension
argument. See below for an example. Note that the word "dimension"
can be abbreviated.
The replacement method ignores any attributes attached to code such
as dimensions or dimnames. In particular, if value is a
DemographicArray object, any metadata is ignored.
library(demdata)
x <- Values(VADeaths2)
x
slab(x, dimension = "age", elements = 1:3)
slab(x, dimension = 1, elements = 1:3)
slab(x, dimension = "age", elements = c("50-54", "70-74"))
slab(x, dimension = "sex", elements = 1)
slab(x, dimension = "sex", elements = 1, drop = FALSE)
slab(x, dimension = "sex", elements = "Male") <- 999
x
slab(x, dimension = "age", elements = "50-54") <- 1:4
x
x1 <- slab(x, dimension = "age", elements = 1, drop = FALSE)
x1
## drop length-one "sex" and "age" dimensions:
slab(x1, dimension = "sex", elements = "Male", drop = TRUE)
## don't drop any length-one dimensions:
slab(x1, dimension = "sex", elements = "Male", drop = FALSE)
## only drop the length-one "sex" dimension:
slab(x1, dimension = "sex", elements = "Male", drop = "dimension")
## abbreviate "dimension"
slab(x1, dimension = "sex", elements = "Male", drop = "dim")