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

Rolf Turner r.turner at auckland.ac.nz
Sun Mar 5 23:49:23 CET 2017


On 04/03/17 06:59, exponential 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!


DO NOT post in HTML.  Your mail was an incomprehensible mess.

cheers,

Rolf Turner

-- 
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276



More information about the R-help mailing list