"[" not "[<-"

Prof Brian Ripley Prof Brian Ripley <ripley@stats.ox.ac.uk>
Wed, 18 Nov 1998 17:59:08 +0000 (GMT)


> Date: Wed, 18 Nov 1998 18:22:23 +0100
> From: Martyn Plummer <plummer@iarc.fr>
> To: Paul Gilbert <pgilbert@bank-banque-canada.ca>
> CC: Prof Brian Ripley <ripley@stats.ox.ac.uk>, R-devel 
<R-devel@stat.math.ethz.ch>
> Subject: Re: "[" not "[<-"
> 
> Paul Gilbert wrote:
> > 
> > Brian Ripley writes
> > >on any of these, but it is less clear what [] should do. As it is not
> > >clear if the attributes apply to the whole object only, I suspect stripping
> > >is right.
> > 
> > I have played with this some and the answer is not obvious. There are some
> > attributes it would be nice to keep and others it would be better to strip. 
My
> > tframe attribute has to be consistent with the first dimension of the 
matrix, so
> > it either has to be modified when the dim is modified or it has to be 
stripped
> > so that there is not conflicting information. I have learned to live with 
the
> > latter.
> 
> Couldn't you use the object-oriented features of R? For objects of class
> "foo", subsetting can be done using the "[.foo" function. Attributes can be
> saved
> before passing the data on to NextMethod (which would strip attributes it
> doesn't
> know about). Then the saved attributes could be restored, possibly in a
> modified
> form, if the object is still of class "foo".
> 
> I had assumed that this was the logic of subsetting, but it would be nice
> if someone could confirm this.
> 

More or less (but I can only confirm my understanding of this). The
saving is not in fact needed. For example, "[.factor" does

        y <- NextMethod("[")
        attr(y, "contrasts") <- attr(x, "contrasts")
        levels(y) <- levels(x)

etc in S. In R it does only attr(y, "levels") <- attr(x, "levels") (and I
think that is now a bug: it should preserve attr(x, "contrasts")).

I was referring to the default: Paul's example had no class.

-- 
Brian D. Ripley,                  ripley@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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._