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

Bert Gunter bgunter.4567 at gmail.com
Mon Mar 6 00:43:37 CET 2017


A great example of why you need to read and follow the posting guide
-- this is a plain text list: NO HTML.

Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Fri, Mar 3, 2017 at 9:59 AM, exponential <exponential at o2.pl> wrote:
> 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]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list