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?
Thanks in advance,
Nick Sabbe
--
ping: nick.sabbe at ugent.be
link: http://biomath.ugent.be
wink: A1.056, Coupure Links 653, 9000 Gent
ring: 09/264.59.36
-- Do Not Disapprove
Using a list as multidimensional indexer
3 messages · Brian Ripley, Nick Sabbe
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
Hm. I got somewhat further:
Ind2<-list(Mm,"b","g")
do.call("[",Ind2)
Seems to work.
However, now I need it one step beyond: in fact, my actual multidimensional
object holds one dimension more than my list holds indexes.
i.e.: I want the equivalent of Mm["a",].
I tried some variants of
Ind3<-list(Mm,"b",NULL)
do.call("[", Ind3)
But all of these return integer(0).
So the actual new question is: how do I pass a 'missing' argument through a
do.call?
Thanks for any pointers,
Nick Sabbe
--
ping: nick.sabbe at ugent.be
link: http://biomath.ugent.be
wink: A1.056, Coupure Links 653, 9000 Gent
ring: 09/264.59.36
-- Do Not Disapprove
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Nick Sabbe
Sent: donderdag 20 januari 2011 11:05
To: r-help at r-project.org
Subject: [R] Using a list as multidimensional indexer
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?
Thanks in advance,
Nick Sabbe
--
ping: nick.sabbe at ugent.be
link: http://biomath.ugent.be
wink: A1.056, Coupure Links 653, 9000 Gent
ring: 09/264.59.36
-- Do Not Disapprove
______________________________________________
R-help at r-project.org mailing list
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.