rvar-slice {posterior}R Documentation

Random variable slicing

Description

Operations for slicing rvars and replacing parts of rvars.

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 rvar.

i, ...

indices; see Details.

value

(rvar or coercable to rvar) Value to insert into x at the location determined by the indices.

drop

(logical) Should singular dimensions be dropped when slicing array rvars? Unlike base array slicing operations, defaults to FALSE.

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:

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:

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]

[Package posterior version 1.6.0 Index]