[Rd] Undesirable behaviour of base::factor

Andrew Gustar @ndrew_gu@t@r @end|ng |rom m@n@com
Thu May 23 18:33:24 CEST 2024


This thread on stackoverflow illustrates the problem... https://stackoverflow.com/questions/78523612/r-factor-from-numeric-vector-drops-every-100-000th-element-from-its-levels

The issue is that factor(), applied to numeric values, uses as.character(), which converts numbers to character strings according to the value of scipen. The stackoverflow thread illustrates a case where this causes some factor levels to become NA. There is also an inconsistency between the treatment of numeric and integer values.

On the face of it, using format(..., scientific = FALSE) instead of as.character() would solve the problem, but this probably needs careful thinking through in case of other side effects!


	[[alternative HTML version deleted]]



More information about the R-devel mailing list