Re: [R] How to make attributes persist after indexing?

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Thu 25 May 2006 - 01:31:08 EST

You could create your own child class with its own [ method.

"[.myfactor" <- function(x, ...) {

       attr <- attributes(x)
       x <- NextMethod("[")
       attributes(x) <- attr
       x

}

gx <- structure(fx, class = c("myfactor", class(fx))) attributes(gx[1])

On 5/24/06, Heinz Tuechler <tuechler@gmx.at> wrote:
> Dear All!
>
> For descriptive purposes I would like to add attributes to objects. These
> attributes should be kept, even if by indexing only part of the object is
> used.
> I noted that some attributes like levels and class of a factor exist also
> after indexing, while others, like comment or label vanish.
> Is there a way to make an arbitrary attribute to be kept after indexing?
> This would be especially useful when indexing a data.frame.
>
> ## example for loss of attributes
> fx <- factor(1:5, ordered=TRUE)
> attr(fx, 'comment') <- 'Comment for fx'
> attr(fx, 'label') <- 'Label for fx'
> attr(fx, 'testattribute') <- 'just for fun'
> attributes(fx) # all attributes are shown
> attributes(fx[]) # all attributes are shown
> attributes(fx[1:5]) # only levels and class are shown
> attributes(fx[1]) # only levels and class are shown
>
> Thanks,
>
> Heinz Tüchler
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu May 25 01:45:52 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 25 May 2006 - 02:10:26 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.