Marc Schwartz (via MN)
2006-May-24 15:30 UTC
[R] How to make attributes persist after indexing?
On Wed, 2006-05-24 at 17:20 +0100, Heinz Tuechler 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"Non-standard" attributes do not survive the use of "[". You could create a new class and subset method for the objects where you require this type of functionality. Frank Harrell has done that in the Hmisc package to support the use of the labeling attributes, which in turn are used by some of his functions such as latex(). You might want to review what he has done in ?label where the "[.labelled" method has been added. Alternatively, you could create your own function to save the attributes, do the subsetting and then restore the attributes to the resultant object. HTH, Marc Schwartz
Gabor Grothendieck
2006-May-24 15:31 UTC
[R] How to make attributes persist after indexing?
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 at 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 at 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 >
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
Thank you for your answer, Gabor. I will see, if I understood it. Heinz At 11:31 24.05.2006 -0400, Gabor Grothendieck wrote:>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 at 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 at 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>> > >