Re: [R] Keep value lables with data frame manipulation

From: Heinz Tuechler <tuechler_at_gmx.at>
Date: Fri 14 Jul 2006 - 09:22:27 EST


At 12:36 13.07.2006 -0400, Richard M. Heiberger wrote:
>> Further I do not see a simple method to label numerical
>> variables. I often encounter discrete, but still metric data, as e.g. risk
>> scores. Usually it would be nice to use them in their original coding,
>> which may include zero or decimal places and to label them at the same
time.
>
>## For this specific case, I use a "position" attribute.
>
>
>tmp <- data.frame(y=rnorm(30), x=factor(rep(c(0,1,2,4,8), 6)))
>attr(tmp$x, "position") <- as.numeric(as.character(tmp$x))
>
>tmp
>as.numeric(tmp$x)
>attr(tmp$x, "position")
>
>bwplot(y ~ x, data=tmp)
>
>panel.bwplot.position <- function(x, y, ..., x.at) {
> for (x.i in x.at) {
> y.i <- y[x.i==x]
> panel.bwplot(rep(x.i, length(y.i)), y.i, ...)
> }
> }
>
>bwplot.position <- function(formula, data, ..., x.at) {
> if (missing(x.at)) {
> x.name <- dimnames(attr(terms(formula),"factors"))[[2]]
> x.at <- attr(data[[x.name]], "position")
> }
> bwplot(formula, data, ...,
> x.at=x.at,
> panel=panel.bwplot.position,
> scales=list(x=list(at=x.at, limits=x.at+c(-1,1))))
>}
>
>bwplot.position(y ~ x, data=tmp)
>
>
>## The above is a simplified version of
>## panel.bwplot.intermediate.hh
>## in the online files for
>## Statistical Analysis and Data Display
>## Richard M. Heiberger and Burt Holland
>## http://springeronline.com/0-387-40270-5
>##
>## An example of a boxplot with both placement and color of the boxes
>## under user control is in
>##
>## http://astro.ocis.temple.edu/~rmh/HH/bwplot-color.pdf
>
Richard,

I recognized your solution already last time you mentioned it and I am thinking about a similar one, (ab)using the names attribute. In principle it seems easy to solve this kind of problems with additional attributes, but without defining a new class and corresponding methods additional attributes get easily lost when indexing/subsetting. The names attribute seems to be rather "resistent". As far as I see, it survives indexing/subsetting and even sorting and this seems to be true also for factors.

Greetings,

Heinz



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 Fri Jul 14 09:30:37 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 Fri 14 Jul 2006 - 12:15:05 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.