Extract with [ on a specified dimension

extract_dim(X, which_dim, idx, drop = NULL, depth = Inf)

extract_rows(X, idx, drop = NULL, depth = Inf)

extract_cols(X, idx, drop = NULL, depth = Inf)

Arguments

X

Typically, an array, but any object with a [ method is accepted (e.g., dataframe, vectors)

which_dim

A scalar integer or character, specifying the dimension to extract from

idx

A numeric, boolean, or character vector to perform subsetting with.

drop

Passed on to [. If NULL (the default), then drop is omitted from the argument, and the default is used (defaults to TRUE for most objects, including arrays)

depth

Scalar number, how many levels to recurse down if X is a list of arrays. Set this if you want to explicitly treat a list as a vector (that is, a one-dimensional array). (You can alternatively set a dim attribute with dim<- on the list to prevent recursion)

Examples

# extract_rows is useful to keep the same code path for arrays of various sizes X <- array(1:8, c(4, 3, 2)) y <- c("a", "b", "c", "d") (Y <- onehot(y))
#> a b c d #> [1,] 1 0 0 0 #> [2,] 0 1 0 0 #> [3,] 0 0 1 0 #> [4,] 0 0 0 1
extract_rows(X, 2)
#> [,1] [,2] #> [1,] 2 6 #> [2,] 6 2 #> [3,] 2 6
extract_rows(Y, 2)
#> a b c d #> 0 1 0 0
extract_rows(y, 2)
#> [1] "b"
library(zeallot) c(X2, Y2, y2) %<-% extract_rows(list(X, Y, y), 2) X2
#> [,1] [,2] #> [1,] 2 6 #> [2,] 6 2 #> [3,] 2 6
Y2
#> a b c d #> 0 1 0 0
y2
#> [1] "b"