[R] Revert a factor to its numeric values

Göran Broström gb at stat.umu.se
Wed Aug 18 16:04:20 CEST 2004


I'm trying a recommendation on the help page for 'factor':

> x <- c(1, 2, 1, 2)
> x <- factor(x, labels = c("one", "two"))
> x
[1] one two one two
Levels: one two
> as.numeric(levels(x))[x]
[1] NA NA NA NA
Warning message:
NAs introduced by coercion
 
Also,

> as.numeric(as.character(x))
[1] NA NA NA NA
Warning message:
NAs introduced by coercion

What am I doing wrong? This is R-1.9.1, Linux (debian installation)

Another question: I have a factor with four levels, which I want 
to collapse to two. How do I do it in the simplest possible way?

Thanks,

Göran 
-- 
 Göran Broström                    tel: +46 90 786 5223
 Department of Statistics          fax: +46 90 786 6614
 Umeå University                   http://www.stat.umu.se/egna/gb/
 SE-90187 Umeå, Sweden             e-mail: gb at stat.umu.se




More information about the R-help mailing list