
Last chance! 50% off unlimited learning
Sale ends in
Extracts one cell per row (column) from a matrix. The implementation is optimized for memory and speed.
rowCollapse(x, idxs, rows = NULL, dim. = dim(x), ...)colCollapse(x, idxs, cols = NULL, dim. = dim(x), ...)
An NxK matrix
.
An index vector
of (maximum) length N (K)
specifying the columns (rows) to be extracted.
Not used.
Returns a vector
of length N (K).
Matrix indexing to index elements in matrices and arrays,
cf. [
().
# NOT RUN {
x <- matrix(1:27, ncol = 3)
y <- rowCollapse(x, 1)
stopifnot(identical(y, x[, 1]))
y <- rowCollapse(x, 2)
stopifnot(identical(y, x[, 2]))
y <- rowCollapse(x, c(1, 1, 1, 1, 1, 3, 3, 3, 3))
stopifnot(identical(y, c(x[1:5, 1], x[6:9, 3])))
y <- rowCollapse(x, 1:3)
print(y)
y_truth <- c(x[1, 1], x[2, 2], x[3, 3], x[4, 1], x[5, 2],
x[6, 3], x[7, 1], x[8, 2], x[9, 3])
stopifnot(identical(y, y_truth))
# }
Run the code above in your browser using DataLab