[Rd] (PR#1558)

Mark.Bravington@csiro.au Mark.Bravington@csiro.au
Thu, 16 May 2002 04:29:03 +0200 (MET DST)


My reading of the R documentation is that the nature of the subscript i in
x[[i]] is not specified; the only obvious rule is that it should lead to to
a single element. Which is what I'm after: a way of extracting or changing
the 4th element of the 2nd element of a list. Incidentally, the operational
difficulty comes in general programming when I don't know in advance how
many levels down the list I'm going. Otherwise I could just grit my teeth &
type x[[ i[1] ]] [[ i[ 2] ]] [[ i[ 3] ]] instead of x[[ i]].

The S2000 documentation says this:

  A special case of the [[ construct is when x is recursive (e.g., a list)
and i is a list. The components of i should all have length 1 and be
character or numeric. The statement x[[i]] is equivalent to:

  x [[ i[[1]] ]] [[ i[[2]] ]]  ...  [[ i[[length(i)]] ]]

  For example, x[[list("b", 2, 4)]] will pick out the 4th component of the
2nd component of the b component of x.
  
In S2000, it turns out that this also works if "i" is a numeric or character
vector rather than a list, as in my example. This extension to non-lists is
useful and harmless but not essential, because one could always use
"as.list". In R, though, neither the list nor the vector version currently
exists.

#If you submit a patch based on the current R-devel sources, we will add it
#if it checks out.  Otherwise, as no one else has ever expressed a need of
#this feature, it is very unlikely to get implemented.  R is not a clone of
#S, and has the features it has because the workers had need of them.

Well, this worker has need of this feature, which is why I wrote those
workarounds! It does seem to me that, if a language allows one to use
recursive structures of arbitrary nestedness, then there should be a simple
mechanism for accessing and changing arbitrary elements of a recursive
structure.

For now, I will just submit my R code as a mini-library. My point about
having a .Primitive was one of speed-- my R code seems to work OK, but it
must slow things down. I doubt that I'm up to producing a C patch-- I use
Delphi not C, so this sounds a bit daunting. If anyone else would like to
pick up the challenge..?

#Please do _not_ use "foo_ ".  It is not a meaningful grouping, and it is
#planned to deprecate `_' in R shortly.

As to "_": I'll be happy to sacrifice the unambiguous "_" provided that the
ambiguous "<-" goes onto the same pyre ;)

cheers

Mark

*******************************

Mark Bravington
CSIRO (CMIS)
PO Box 1538
Castray Esplanade
Hobart
TAS 7001

phone (61) 3 6232 5118
fax (61) 3 6232 5012
Mark.Bravington@csiro.au 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._