[R] coercing factors to matrix() --> num/char ? -- inconsistencies|?

Martin Maechler maechler at stat.math.ethz.ch
Wed Jun 23 10:02:07 CEST 1999

The old factor() wars......
    {{maybe don't just report that factor()s are broken by design..}}

If ff is a factor, e.g.,
    ff <- as.factor(rep(1:2,3))
    f2 <- ff; levels(f2) <- c("Lo","Hi")

1) we don't allow {in R as in S-plus 3.x} arithmetic on factors, i.e.
     1 + ff
  gives an error, and I think most of us believe this is as desired.
  {{if one really wants the factor codes, use   codes(ff) ...}}.
  [BTW: S-plus 5.0r3 doesn't give an error anymore but silently returns NA,
	i.e.,  rep(NA,6) for the above example

2) On the other hand, it seems that implicit coercion to character()
   is rather ok, such as

   > paste("A", f2)

   [1] "A Lo" "A Hi" "A Lo" "A Hi" "A Lo" "A Hi"


>From this logic, coercing to a matrix should coerce to character rather
than integer/numeric codes.

A: both R and S-plus 3.4  give an INTEGER matrix (with codes(.)) for


B: R gives an integer matrix  but S-plus a character one for


So far the current behavior which I don't like.
I believe all matrix coercions should return mode "character" objects
for consistency with other coercion behavior of factors.

S(-plus) seems particularly inconsistent in doing different coercions
for cbind() and matrix().

Opinions ?   Proposals ?

Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum SOL G1;	Sonneggstr.33
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1086			<><
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list