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

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Jun 23 12:17:02 CEST 1999


Martin Maechler <maechler at stat.math.ethz.ch> writes:

[On R-help, not -devel??]

> The old factor() wars......
>     {{maybe don't just report that factor()s are broken by design..}}
...
> 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) ...}}.

As.integer(), you mean. Codes() has some "portable brain damage"
associated with it.

We do however have indexing with implicit integer coercion, e.g.

> ff<-factor(1:3,labels=3:1)  
> letters[ff]
[1] "a" "b" "c"

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

Hm. I'm not sure there really is much logic in this... Factors are
sometimes character-like, sometimes integer-like.

> 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)
...
> 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 ?

Not really sure whether the problem is that R is consistent with
itself or inconsistent with S. If we change as you suggest, we'll
have two incompatibilities instead of one...

In what kinds of code would this become relevant? 

BTW, we also have this little item:

> matrix(ff)
     [,1]
[1,]    1
[2,]    2
[3,]    3
> array(ff,c(3,1))
     [,1]
[1,] "3" 
[2,] "2" 
[3,] "1" 

(In Splus, matrix() *calls* array() so this won't happen)

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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