[R] How to keep attributes when dropping factor levels?

Heinz Tuechler tuechler at gmx.at
Thu Mar 19 16:23:26 CET 2009


Dear Dieter,

did you try it? I kew this post and below I extended my example. The 
result seems to be identical. Maybe that other attributes of the _DF_ 
don't get lost, but the label of the factor does still disappear.

ff <- factor(substring("statistics", 1:10, 1:10), levels=letters)
attributes(ff)$label <- 'test label'
attributes(ff)$label
gg <- ff[, drop=TRUE]
attributes(gg)$label
hh <- factor(ff)
attributes(hh)$label
ii <- car:::recode(ff, "'t'='s'")
attributes(ii)$label

DF <- data.frame(ff)
attributes(DF$ff)$label
DF[] <- lapply(DF, "[", drop=TRUE)
attributes(DF$ff)$label

Thanks,

Heinz

At 14:36 19.03.2009, Dieter Menne wrote:
>Heinz Tuechler <tuechler <at> gmx.at> writes:
>
> >
> > to drop unused factor levels two ways are outlined in R-help. In both
> > cases a label attribute is lost.
>
>Brian Ripley has posted this
>
>http://markmail.org/message/pl2odydwzv64v3u3
>
>Dieter

Dear All,

to drop unused factor levels two ways are outlined in R-help. In both 
cases a label attribute is lost.
The same happens, when using car:::recode.
Is there a simple way to avoid losing attributes?

Thanks,

Heinz

## example
ff <- factor(substring("statistics", 1:10, 1:10), levels=letters)
attributes(ff)$label <- 'test label'
attributes(ff)$label
gg <- ff[, drop=TRUE]
attributes(gg)$label
hh <- factor(ff)
attributes(hh)$label
ii <- car:::recode(ff, "'t'='s'")
attributes(ii)$label

 > version
                _
platform       i386-pc-mingw32
arch           i386
os             mingw32
system         i386, mingw32
status         Patched
major          2
minor          8.1
year           2009
month          03
day            13
svn rev        48132
language       R
version.string R version 2.8.1 Patched (2009-03-13 r48132)
 > sessionInfo()
R version 2.8.1 Patched (2009-03-13 r48132)
i386-pc-mingw32

locale:
LC_COLLATE=German_Austria.1252;LC_CTYPE=German_Austria.1252;LC_MONETARY=German_Austria.1252;LC_NUMERIC=C;LC_TIME=German_Austria.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] car_1.2-12




More information about the R-help mailing list