Dario Strbenac
2017-Jun-16 06:00 UTC
[Rd] Simplify and By Convert Factors To Numeric Values
Good day, It's not described anywhere in the help page, but tapply and by functions will, by default, convert factors into numeric values. Perhaps this needs to be documented or the behaviour changed.> tapply(1:3, 1:3, function(x) factor(LETTERS[x], levels = LETTERS))1 2 3 1 2 3 The documentation states "... tapply returns a multi-way array containing the values ..." but doesn't mention anything about converting factors into integers. I'd expect the values to be of the same type. R version 3.4.0 (2017-04-21) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Debian GNU/Linux 8 (jessie) Matrix products: default BLAS: /dskh/nobackup/biostat/software/R-3.4.0/lib/libRblas.so LAPACK: /dskh/nobackup/biostat/software/R-3.4.0/lib/libRlapack.so -------------------------------------- Dario Strbenac University of Sydney Camperdown NSW 2050 Australia
Charles C. Berry
2017-Jun-16 19:12 UTC
[Rd] Simplify and By Convert Factors To Numeric Values
On Fri, 16 Jun 2017, Dario Strbenac wrote:> Good day, >> It's not described anywhere in the help page, but tapply and by > functions will, by default, convert factors into numeric values. Perhaps > this needs to be documented or the behaviour changed.It *is* described in the help page. This returns a list of objects and each object class has "factor" tapply(rep(1:2,2), rep(1:2,2), function(x) factor(LETTERS[x], levels = LETTERS)) and this> >> tapply(1:3, 1:3, function(x) factor(LETTERS[x], levels = LETTERS)) > 1 2 3 > 1 2 3returns a vector object with no class.>> The documentation states "... tapply returns a multi-way array > containing the values ..." but doesn't mention anything about converting > factors into integers. I'd expect the values to be of the same type.and also states "If FUN returns a single atomic value for each such cell ... and when simplify is TRUE ... if the return value has a class (e.g., an object of class "Date") the class is discarded." which is what just happened in your example. Maybe you want: unlist(tapply(1:3, 1:3, function(x) factor(LETTERS[x], levels = LETTERS),simplify=FALSE)) Trying to preserve class worked here in a way you might have hoped/expected, but might lead to difficulties in other uses. HTH, Chuck