I have following array (in an example 3-dimensional, but it can be 3+ dims):
a <- c('a1', 'a2', 'a3')
b <- c('bb', 'bbb')
c <- c('C', 'CC', 'CCC')
dimNa <- list('a' = a, 'b' = b, 'c' = c)
outputArray <- array(NA,
unname(sapply(dimNa, function(x) length(x), simplify = T)),
unname(dimNa))
I can subset it using name from one dimension manually, like:
> outputArray[,'bb',]
C CC CCC
a1 NA NA NA
a2 NA NA NA
a3 NA NA NA
or
> outputArray[,,'CCC']
bb bbb
a1 NA NA
a2 NA NA
a3 NA NA
The question is how to pass a vector (or sth else? - in this case ,'bb',
and ,,'CCC'
) to [
, so I can write a function to generate this automatically (assuming that I can extract info regarding which name is stored in which dimension - names are unique and I can get this info from dimnames(outputArray)
)? Just like I mentioned array can be 3+ dims.
Edit: I would like to subset by name only from one dimension. So outputArray[,'bb',]
or outputArray[,,'CCC']
, not outputArray[,'bb','CCC']
, but solution should work for more dimensions.