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

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


"Z. Todd Taylor" <Todd.Taylor at pnl.gov> writes:

> I'd be interested to hear a discussion of when they need to
> behave like integers.  In my work I can't ever remember needing
> anything other than the character representation in my "user
> level" coding.  I can see the need to access the integers for
> "lower level" programming, for example, but when else?
> 
> (My thought has always been that factors should *always* coerce
> to character unless the user explicitly requests otherwise.)

The main one is probably indexing:

> ff<-factor(rbinom(20,1,.5),levels=0:1)
> x<-rnorm(20)
> y<-rnorm(20)
> plot(x,y,pch=c("f","m")[ff])

.which BTW also applies to the cbind issue since you can do things like:

> ff<-factor(rbinom(10,1,.5),levels=0:1)
> gg<-factor(rbinom(10,1,.5),levels=0:1)

> data.frame(ff,gg,comb.code=matrix(1:4,2)[cbind(ff,gg)])
   ff gg comb.code
1   0  1         3
2   1  0         2
3   1  0         2
4   0  1         3
5   0  1         3
6   0  0         1
7   1  1         4
8   0  0         1
9   1  0         2
10  1  1         4

-- 
   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