Dear R-core! I have written a function mapLevels() to map levels of a factor. It can produce a map (internally a list) of factor levels and internal integer codes. I call this integer levelsMap. There is also character levelsMap, but not of importance at this moment. I found out that it was quite straightforward to write c() method for factors with use of mapLevels() and Iam asking here if there is any interest to include this into base R.
In base R c() on two factors coerces to internal integer codes > f1 <- factor(c("A", "B")) > f2 <- factor(c("A", "C")) > f1 [1] A B Levels: A B > f2 [1] A C Levels: A C > c(f1, f2) [1] 1 2 1 2 or character vector if one of arguments is character c(as.character(f1), f2) [1] "A" "B" "1" "2" this is OK, but not really wanted by the user. Code bellow show my implementation of c.factor(). For the moment mapLevels() and c.factor() live in gdata package (from version 2.3.1) - it has ben submitted to CRAN last night. > install.packages("gdata") > library(gdata) ## c.factor is not exported > gdata:::c.factor(f1, f2) [1] A B A C Levels: A B C > gdata:::c.factor(as.character(f1), f2) [1] A B A C Levels: A B C > gdata:::c.factor(f1, as.character(f2)) [1] A B A C Levels: A B C > gdata:::c.factor(as.integer(f1), f2) Error in mapLevels.default(X[[1]], ...) : mapLevels can only be used on “factor” and “character” atomic 'x' As can be seen characters are handled in the same way as factors. Does anyone see any value in this? -- Lep pozdrav / With regards, Gregor Gorjanc ---------------------------------------------------------------------- University of Ljubljana PhD student Biotechnical Faculty Zootechnical Department URI: http://www.bfro.uni-lj.si/MR/ggorjan Groblje 3 mail: gregor.gorjanc <at> bfro.uni-lj.si SI-1230 Domzale tel: +386 (0)1 72 17 861 Slovenia, Europe fax: +386 (0)1 72 17 888 ---------------------------------------------------------------------- "One must learn by doing the thing; for though you think you know it, you have no certainty until you try." Sophocles ~ 450 B.C. ______________________________________________ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel