Hi R-experts! Every once in a while I need to convert a factor to a vector of numeric values. as.numeric(myfactor) of course returns a nice numeric vector of the indexes of the levels which is usually not what I had in mind:> v <- c(25, 3.78, 16.5, 37, 109) > f <- factor(v) > f[1] 25 3.78 16.5 37 109 Levels: 3.78 16.5 25 37 109> as.numeric(f)[1] 3 1 2 4 5>What I really want is a function "unfactor" that returns v:> unfactor(f)[1] 25.00 3.78 16.50 37.00 109.00 Of course I could use something like> as.numeric(levels(f)[as.integer(f)])But I just can't believe there is no R function to do this in a more readable way. Actually, the behaviour of as.numeric() doesn't strike me as very intuitive. I'm sure it has been implemented that way for a reason - but what is it? cu Philipp -- Dr. Philipp Pagel Tel. +49-89-3187-3675 Institute for Bioinformatics / MIPS Fax. +49-89-3187-3585 GSF - National Research Center for Environment and Health Ingolstaedter Landstrasse 1 85764 Neuherberg Germany
See the FAQ, Q7.12. On Wed, 4 Jun 2003, Philipp Pagel wrote:> Every once in a while I need to convert a factor to a vector of numeric > values. as.numeric(myfactor) of course returns a nice numeric vector of > the indexes of the levels which is usually not what I had in mind:... It's done that way because that is how it is defined to work in S, and lots of code relies on it. -- 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 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Philipp Pagel <p.pagel at gsf.de> writes:> But I just can't believe there is no R function to do this in a more > readable way. Actually, the behaviour of as.numeric() doesn't strike me > as very intuitive. I'm sure it has been implemented that way for a > reason - but what is it?One reason is S compatibility, as Brian pointed out. But there is also the point that you can *always* convert a factor to its underlying integer values, but only *sometimes* convert the level names. Generally we prefer code that fails more rarely... as.numeric(as.character(f)) works too, although not quite as efficiently as as.numeric(levels(f))[f]. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907