I have a multidimensional array, created in the following way:
my_array <- array(seq(12), dim=my_dims, dimnames=my_dimnames)
where "my_dims" is a numeric vector and "my_dimnames" is a list of character vectors, with lengths corresponding to the dimensions specified in "mydims", for example:
my_dims <- c(2, 3, 2)
my_dimnames <- list(c("D_11", "D_12"), c("D_21", "D_22", "D_23"), c("D_31", "D_32"))
So that "my_array" looks something like this:
, , D_31
D_21 D_22 D_23
D_11 1 3 5
D_12 2 4 6
, , D_32
D_21 D_22 D_23
D_11 7 9 11
D_12 8 10 12
Additionally, I have a character vector containing specific values of each dimension:
my_values <- c("D_11", "D_21", "D_32")
Now, from this question: R - how to get a value of a multi-dimensional array by a vector of indices I learned that I can read and write values from and to specific cells in "my_array" using matrix indexing, like this:
my_array[matrix(my_values, 1)]
to get the value "7" and
my_array[matrix(my_values, 1)] <- 1
to set that same cell to value "1"
What I don't understand, though, is how I can get all values from a specific dimension from "my_array", given specific values for all other dimensions using this method.
For example: how can I get a vector containing all values of the first dimension with fixed values "D_21" and "D_32" for the second and third dimension? So in this case what I try to extract would be a vector of the form:
c(7, 8)
How do I need to adjust the matrix indexing to achieve this result?
Thanks a lot in advance!
dimnamesof your array? Something like:my_array[cbind(dimnames(my_array)[[1]], "D_21", "D_32")]perhaps?do.call("[", ... To simulate the missing argument in a dimensions usesubstitute(). E.g.do.call("[", c(list(my_array), list("D_11", "D_23", "D_31")));do.call("[", c(list(my_array), list(substitute(), "D_23", "D_31")));do.call("[", c(list(my_array), list("D_12", substitute(), "D_31", drop = FALSE)))etc. Your arguments fordo.callneed to be in a "list"; e.g. for "my_values" you'd needas.list(my_values).