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

Martin Maechler Martin Maechler <>
Tue, 20 Jan 1998 09:37:53 +0100

>From  R-core;  this should interest most R-devel'ers (to some extent):

Since 0.60,  the semantics of   as.numeric(<factor>)  has changed,

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

as explained by Ross, below :

>>>>> "KH" == Kurt Hornik <> writes:

>>>>> Ross Ihaka writes:
    KH>> From Mon Jan 19 22:52 NZD 1998 Subject:
    KH>> Difference R/S
    KH>> Andreas just pointed me to the following:
    KH>> v <- as.factor(c("Age","Number","Age")) as.numeric(v)
    KH>> gives
    KH>> [1] 1 2 1
    KH>> in S+ and
    KH>> [1] NA NA NA
    KH>> Bug/feature/intentional?
    KH>> Of course, R makes more sense because as.numeric("Age") gives NA in
    KH>> both R and S+ ...
    KH>> Or, should we have as.numeric() return the codes on a non-numeric
    KH>> factor?

    Ross> At present R (implicitly) computes as.numeric(x) for x a factor as

    Ross> 	as.numeric(as.character(x))

    Ross> and S computes

    Ross> 	codes(x)

    Ross> I mistakenly thought that S does what I have implemented for R.
    Ross> Thomas first objected to the difference and then said he quite liked
    Ross> it.

    Ross> I quite like the present semantics, but it is easy to change if
    Ross> others have different preferences.

    KH> I personally think that the current R approach makes more sense,
    KH> too.  If we all agree on it, I would like to add the difference to
    KH> the FAQ, so that it is (well) documented.

Hmm,  I first had advocated your view above, myself.

Later, I started to discover in how much S-code
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).

r-devel mailing list -- Read
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: