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

Martin Maechler (maechler@stat.math.ethz.ch)
Wed, 23 Jun 1999 10:02:07 +0200

Date: Wed, 23 Jun 1999 10:02:07 +0200
Message-Id: <199906230802.KAA19137@sophie.ethz.ch>
From: Martin Maechler <maechler@stat.math.ethz.ch>
To: R-help@stat.math.ethz.ch
Subject: [R] coercing factors to matrix() --> num/char ? -- inconsistencies|?

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.
However,

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

rbind(ff)
cbind(f2)

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

matrix(ff)
or
matrix(f2)

-------
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@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@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


This archive was generated by hypermail 1.02.