Message-ID: <alpine.LFD.2.00.1101201017360.9376@toucan.stats.ox.ac.uk>
Date: 2011-01-20T10:20:33Z
From: Brian Ripley
Subject: Using a list as multidimensional indexer
In-Reply-To: <03d001cbb889$7c0e8db0$742ba910$@sabbe@ugent.be>
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