[R] Using a list as multidimensional indexer

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Jan 20 11:20:33 CET 2011


On Thu, 20 Jan 2011, Nick Sabbe wrote:

> Hello list.
>
> Another 'puzzle' for which I don't have a clean solution.
> Say I have a multidimensional object, e.g.:
> Mm<-matrix(1:6, nrow=2, dimnames=list(c("a","b"), c("g","h","i")))
> And on the other hand I have a list
> Ind<-list("b","g")
> This holds, for each dimension, an indexer for that dimension.
> Now I would like to get the element pointed at by the list.
> The obvious solutions don't seem to work, and I can't seem to get do.call to
> call the indexer ('[') on my multidimensional object.
>
> Any suggestions?

> do.call(`[`, c(quote(Mm), Ind))
[1] 2
> do.call(`[`, c(list(Mm), Ind))
[1] 2

gives

> Mm["b", "g"]
[1] 2

The first refers to the object by name, the second uses it directly.


> Thanks in advance,
>
> Nick Sabbe
> --
> ping: nick.sabbe at ugent.be
> link: http://biomath.ugent.be

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list