as.numeric(<factor>) [Difference R/S]

Peter Dalgaard BSA p.dalgaard@biostat.ku.dk
20 Jan 1998 11:44:51 +0100


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

> 
> From  R-core;  this should interest most R-devel'ers (to some extent):
> 
> Since 0.60,  the semantics of   as.numeric(<factor>)  has changed,
> e.g.
> 
> R> as.integer(factor(c("A","BB")))
> [1] NA NA
> R> as.integer(factor(c(100,40,100)))
> [1] 100  40 100
> 
> whereas older R and S:
> 
> S> as.integer(factor(c("A","BB")))
> [1] 1 2
> S> as.integer(factor(c(100,40,100)))
> [1] 2 1 2
> 
...
> 
> Hmm,  I first had advocated your view above, myself.
> 
> Later, I started to discover in how much S-code
> 	as.numeric(ff) 
> is just used to extract the factor codes (in {1:M})  from a factor.
> 
> This lead me (and Peter Dalgaard, I think) to the conclusion that
> - yes, the present R behavior maybe ``cleaner'' than S's
> - no, it is a pain to keep it, because it breaks S code too often.
> 
> However, as you see, we haven't agreed yet on the topic.
> I think we should agree ASAP, since it involves code in several places
> (outside R base).

Actually, I'm even stronger in favour of the S semantics. In addition
to the above

	- you can always get current behaviour with
        as.numeric(as.character(f)) or as.numeric(levels(f))[f]

	- one should avoid generating NA's unless absolutely necessary

	- when a factor is used for subscripting, you mean the codes,
        not the levels. Currently, we have

> (1:5)[factor(1:5,labels=5:1)]
[1] 1 2 3 4 5

but

> as.numeric(factor(1:5,labels=5:1))
[1] 5 4 3 2 1

	I.e. *sometimes* when a factor is coerced to numeric you get
	something different. (And if you change the index semantics,
	code for trend tests and the like is likely to break!). 
-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907

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