On Wed, 18 Aug 2004, G??ran Brostr??m wrote:> 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)Your factor is made up of "one", "two", which are not numeric -- don't expect R to speak English (or Swedish). You could just as easily have used labels = c("apples", "oranges").> 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?via levels<- : there is an example on the help page for levels. -- 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
gb at stat.umu.se (G??ran Brostr??m) writes:> 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)You appear to be assuming tha R knows how to make numbers from text strings as.numeric(c("jedan","dva","tri","ceteri")) is not going to give you 1,2,3,4 either (not even in a Croatian locale). The suggestion on the help page works for numeric levels:> x <- c(1, 2, 1, 2) > x <- factor(x, labels = c("5", "7")) > as.numeric(levels(x))[x][1] 5 7 5 7> 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?> x <- factor(c("jedan","dva","tri","ceteri")) > x[1] jedan dva tri ceteri Levels: ceteri dva jedan tri> levels(x) <- c("even","even","odd","odd") > x[1] odd even odd even Levels: even odd -- 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
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
On Wed, 18 Aug 2004, G??ran Brostr??m wrote:> 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 >usually when people want to "revert a factor to its numeric values" they mean that the labels are numbers and they want those numbers. In that case as.numeric(x) or unclass(x) are wrong because they give you the underlying codes. You, somewhat unusually, actually want the underlying codes, which you get with unclass(x). -thomas