[Rd] suggestion for extending ?as.factor

Petr Savicky savicky at cs.cas.cz
Sun May 3 22:32:04 CEST 2009


In R-2.10.0, the development version, function as.factor() uses 17 digit
precision for conversion of numeric values to character type. This
is very good for the consistency of the resulting factor, however,
i expect that people will complain about, for example, as.factor(0.3)
being
  [1] 0.29999999999999999
  Levels: 0.29999999999999999

I suggest to extend the "Warning" section of ?as.factor by the following
paragraph.

If as.factor() is used for a numeric vector, then the numbers are
converted to character strings with 17 digit precision using their
machine representation. This guarantees that different numbers are
converted to different levels, but may produce unwanted results, if
the numbers are expected to have limited number of decimal positions.
For example, as.factor(c(0.1, 0.2, 0.3)) produces
  [1] 0.10000000000000001 0.20000000000000001 0.29999999999999999
  Levels: 0.10000000000000001 0.20000000000000001 0.29999999999999999
In order to avoid this, convert the numbers to a character vector
using formatC() or a similar function before using as.factor().

Petr.



More information about the R-devel mailing list