[R] factor documentation issue

From: Geoff Russell <geoffrey.russell_at_gmail.com>
Date: Tue 27 Feb 2007 - 23:11:19 GMT

There is a warning in the documentation for ?factor (R version 2.3.0) as follows:

" The interpretation of a factor depends on both the codes and the   '"levels"' attribute. Be careful only to compare factors with the   same set of levels (in the same order). In particular,   'as.numeric' applied to a factor is meaningless, and may happen by   implicit coercion. To "revert" a factor 'f' to its original   numeric values, 'as.numeric(levels(f))[f]' is recommended and   slightly more efficient than 'as.numeric(as.character(f))'.

But as.numeric seems to work fine whereas as.numeric(levels(f))[f] doesn't always do anything useful.

For example:

> f<-factor(1:3,labels=c("A","B","C"))
> f

[1] A B C
Levels: A B C
> as.numeric(f)

[1] 1 2 3
> as.numeric(levels(f))[f]
[1] NA NA NA
Warning message:
NAs introduced by coercion

And also,

> f<-factor(1:3,labels=c(1,5,6))
> f

[1] 1 5 6
Levels: 1 5 6
> as.numeric(f)

[1] 1 2 3
> as.numeric(levels(f))[f]
[1] 1 5 6

Is the documentation wrong, or is the code wrong, or have I missed something?

Geoff Russell

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed Feb 28 10:15:33 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 28 Feb 2007 - 06:30:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.