Learn R Programming

lpjmlkit (version 1.0.11)

asub: Subset a named array

Description

Subset an array with the supplied dimnames and - if defined - replace values.

Usage

asub(x, ..., drop = TRUE)

Value

array (or vector if drop = TRUE and only one dimension is left) of the selected subset of x.

Arguments

x

An array with named dimensions.

...

One or several vectors of indices or character strings to be used to subset x. Argument names refer to the dimension name to be subset, while argument values specify the selected elements along the respective dimension. Examples: cell = c(27411:27416), band = -c(14:16, 19:32), band = c("rainfed rice","rainfed maize").

drop

Logical. If TRUE (default), dimensions with a length of 1 are dropped from the result. Otherwise, they are kept.

Examples

Run this code
my_array <- array(1,
                  dim=c(cell = 67, month = 12, band = 3),
                  dimnames = list(cell = 0:66,
                                month = 1:12,
                                band = c("band1", "band2", "band3")))
my_subset <- asub(my_array,
                  band = c("band1", "band3"))
dimnames(my_subset)[3]
# $ band
#   [1] "band1"
#   [2] "band3"

Run the code above in your browser using DataLab