[R] How to make attributes persist after indexing?

Heinz Tuechler tuechler at gmx.at
Wed May 24 18:20:45 CEST 2006

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


Heinz Tüchler

More information about the R-help mailing list