[R] array - how to create "logical expression" for subset dynamically

exponential exponential at o2.pl
Fri Mar 3 18:59:49 CET 2017


Hi!   I've tried on SO, but without success. Maybe you will be able to help.   I have the following array (I use 3-dimensional in this example for simplicity, but it can be 3+ dims - 10,11 or more):   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   I would like to write a function which subsets particular named element from one dimension (I mean I don't expect one dimension as a result by slicing using one named element, if that makes sense). Something like     myfunc <- function(inputArray, namedElement)   thus I can call it (using above example):   myfunc(outputArray, 'bb')   or    myfunc(outputArray, 'CCC')   to get identical as above results.   I know how to deal with finding to which dimension "namedElement" belongs (in my case all names are unique).   The question is how to subset it from the array? One idea is to construct what is inside [ ] (in example: ,'bb', or ,,'CCC'). I've tried something like this:    inputDims <- ",,'CCC'"   outputArray[parse(text=inputDi   But this doesn't work. Maybe different approach should be used...   Any ideas are welcome:)!   Cheers!

	[[alternative HTML version deleted]]



More information about the R-help mailing list