[R] Keep value lables with data frame manipulation
Heinz Tuechler
tuechler at gmx.at
Fri Jul 14 01:22:27 CEST 2006
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
More information about the R-help
mailing list