[R] How to change the class of data?

Philipp Pagel p.pagel at wzw.tum.de
Thu Jun 12 10:11:33 CEST 2008

On Thu, Jun 12, 2008 at 03:42:23AM -0400, Charilaos Skiadas wrote:
> Seeing how there have been three wrong answers so far, I should point  
> out that:
> 1) This is an FAQ:  http://cran.r-project.org/doc/FAQ/R-FAQ.html#How- 
> do-I-convert-factors-to-numeric_003f

Going over the r-help archive, we have seen the misunderstanding
you are pointing out quite often - it clearly is a FREQUENTLY asked
question). In my experience, it is also among the top-five confusions
among students I have introduced R to. And I clearly remember my own
confusion about as.numeric(some_factor) returning the internal encoding
rather than what I had expected, when I started with R.

I guess there is no way to change that behaviour without breaking
existing code, but I feel it would have been much better to have
as.numeric and as.integer do what people expect and have something like
levelencoding(some_factor) for getting the integer representation. 
The problem is particularly frustrating as the result of type casting is
inconsistent: as.character does exactly what people's intuition says
(i.e. operate on the levels) while as.numeric does not.

So what is my point? I guess it's my message for all new R-users: 
"You are not alone - this has confused all of us in the beginning."

Maybe as.integer and as.numeric should give a warning whenever applied to
a factor?


Dr. Philipp Pagel
Lehrstuhl für Genomorientierte Bioinformatik
Technische Universität München
Wissenschaftszentrum Weihenstephan
85350 Freising, Germany

More information about the R-help mailing list