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

Heinz Tuechler tuechler at gmx.at
Thu Mar 19 12:11:24 CET 2009


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