[R] converting numerical parts of factors into numeric vectors

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Apr 28 14:32:49 CEST 2000


> From: Jan.Schelling at hydro.com
> Date: Fri, 28 Apr 2000 13:45:43 +0100
> 
> Try:
> 
> as.numeric(levels(x)[x])
> 
> However, this gives you a warning if you have any non-numerical levels as
> mentioned in the help file for 'as.numeric'.


This is frequently mentioned here.  The simplest idea is

as.numeric(as.character(x))

but more efficient might be

as.numeric(levels(x))[x]

since that converts each string once only.  You would have to try hard
to find a noticeable difference, though.

> x <- factor(rep(123, 10000))
> system.time(zz <- as.numeric(as.character(x)))
[1] 0.08 0.01 0.09 0.00 0.00
> system.time(zz <- as.numeric(levels(x))[x])   
[1] 0.02 0.00 0.02 0.00 0.00
> system.time(zz <- as.numeric(levels(x)[x]))
[1] 0.08 0.01 0.09 0.00 0.00


BTW, all methods are likely to give warnings, but you can turn warnings
off if you want

> x <- factor(c(1:3, "four"))
> as.numeric(as.character(x))
[1]  1  2  3 NA
Warning message: 
NAs introduced by coercion 
> options(warn=-1)
> as.numeric(as.character(x))
[1]  1  2  3 NA


> From: wolters at ikp.uni-bonn.de on 2000-04-28 10:23 GMT
> I checked CRAN, V&R, and the online help, but to no avail.

I can't resist: p.15 of V&R's `S Programming' says

   One point to watch is that if it is necessary to convert a factor with
   numeric levels to a numeric vector, this must be done by
   
   as.numeric(as.character(x))
   
   which will convert any non-numeric values to NAs ...


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

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



More information about the R-help mailing list