rvar-slice {posterior} | R Documentation |
Random variable slicing
Description
Operations for slicing rvar
s and replacing parts of rvar
s.
Usage
## S3 method for class 'rvar'
x[[i, ...]]
## S3 replacement method for class 'rvar'
x[[i, ...]] <- value
## S3 method for class 'rvar'
x[..., drop = FALSE]
## S3 replacement method for class 'rvar'
x[i, ...] <- value
Arguments
x |
an |
i , ... |
indices; see Details. |
value |
( |
drop |
(logical) Should singular dimensions be dropped when slicing
array |
Details
The rvar
slicing operators ([
and [[
) attempt to implement the same
semantics as the base array slicing operators. There are some
exceptions; most notably, rvar
slicing defaults to drop = FALSE
instead
of drop = TRUE
.
Extracting or replacing single elements with [[
The [[
operator extracts (or replaces) single elements. It always
returns (or replaces) a scalar (length-1) rvar
.
The x[[i,...]]
operator can be used as follows:
-
x[[<numeric>]]
for scalar numerici
: gives thei
th element ofx
. Ifx
is multidimensional (i.e.length(dim(x)) > 1
), extra dimensions are ignored when indexing. For example, ifx
is a6 \times 2
rvar
array, the 7th element,x[[7]]
, will be the first element of the second column,x[1,2]
. -
x[[<numeric rvar>]]
for scalar numericrvar
i
: a generalization of indexing wheni
is a scalar numeric. Within each draw ofx
, selects the element corresponding to the value ofi
within that same draw. -
x[[<character>]]
for scalar characteri
: gives the element ofx
with name equal toi
. Unlike with base arrays, does not work with multidimensionalrvar
s. -
x[[i_1,i_2,...,i_n]]
for scalar numeric or characteri_1
,i_2
, etc. Must provide exactly the same number of indices as dimensions inx
. Selects the element at the corresponding position in thervar
by number and/or dimname (as a string).
Extracting or replacing multiple elements with [
The [
operator extracts (or replaces) multiple elements. It always returns
(or replaces) a possibly-multidimensional rvar
.
The x[i,...]
operator can be used as follows:
-
x[<logical>]
for vector logicali
:i
is recycled to the same length asx
, ignoring multiple dimensions inx
, then anrvar
vector is returned containing the elements inx
wherei
isTRUE
. -
x[<logical rvar>]
for scalar logicalrvar
i
: returns anrvar
the same shape asx
containing only those draws wherei
isTRUE
. -
x[<numeric>]
for vector numerici
: anrvar
vector is returned containing thei
th elements ofx
, ignoring dimensions. -
x[<matrix>]
for numeric matrixi
, wherencol(i) == length(dim(x))
: each row ofi
should give the multidimensional index for a single element inx
. The result is anrvar
vector of lengthnrow(i)
containing elements ofx
selected by each row ofi
. -
x[i_1,i_2,...,i_n]
for vector numeric, character, or logicali_1
,i_2
, etc. Returns a slice ofx
containing all elements from the dimensions specified ini_1
,i_2
, etc. If an argument is left empty, all elements from that dimension are included. Unlike base arrays, trailing dimensions can be omitted entirely and will still be selected; for example, ifx
has three dimensions, bothx[1,,]
andx[1,]
can be used to create a slice that includes all elements from the last two dimensions. Unlike base arrays,[
defaults todrop = FALSE
, so results retain the same number of dimensions asx
.
Examples
x <- rvar(array(1:24, dim = c(4,2,3)))
dimnames(x) <- list(c("a","b"), c("d","e","f"))
x
## Slicing single elements
# x[[<numeric>]]
x[[2]]
# x[[<numeric rvar>]]
# notice the draws of x[1:4]...
draws_of(x[1:4])
x[[rvar(c(1,3,4,4))]]
# ... x[[rvar(c(1,3,4,4))]] creates a mixures of those draws
draws_of(x[[rvar(c(1,3,4,4))]])
# x[[i_1,i_2,...]]
x[[2,"e"]]
## Slicing multiple elements
# x[<logical>]
x[c(TRUE,TRUE,FALSE)]
# x[<logical rvar>]
# select every other draw
x[rvar(c(TRUE,FALSE,TRUE,FALSE))]
# x[<numeric>]
x[1:3]
# x[<matrix>]
x[rbind(
c(1,2),
c(1,3),
c(2,2)
)]
# x[i_1,i_2,...,i_n]
x[1,]
x[1,2:3]
x[,2:3]