Hola! The R FAQ says: 7.12 How do I convert factors to numeric? It may happen that when reading numeric data into R (usually, when reading in a file), they come in as factors. If f is such a factor object, you can use as.numeric(as.character(f)) to get the numbers back. More efficient, but harder to remember, is as.numeric(levels(f))[as.integer(f)] In any case, do not call as.numeric() or their likes directly. But trying to follow the advice: (this is without package method attached, but the results are the same with): First doing as one shouldn't:> table( as.numeric(EdadC) )1 2 3 4 5 20 99 157 127 74 Doing as the FAQ says:> table( as.numeric(as.character(EdadC)) )character(0) Warning message: NAs introduced by coercion or:> table( as.numeric(levels(EdadC))[as.integer(EdadC)] )character(0) Warning message: NAs introduced by coercion ? Kjetil Halvorsen
Kjetil - EdadC seems to have only five levels, anyway. What are those five levels ? Are they strings which it would make sense to interpret as numeric ? as.numeric() obviously thinks they are not. - tom blackwell - u michigan medical school - ann arbor - On Thu, 21 Aug 2003, kjetil brinchmann halvorsen wrote:> 7.12 How do I convert factors to numeric? > > It may happen that when reading numeric data into R (usually, when > reading in a file), they come in as factors. If f is such a factor > object, you can use > > as.numeric(as.character(f)) > > to get the numbers back. More efficient, but harder to remember, is > > as.numeric(levels(f))[as.integer(f)] > > In any case, do not call as.numeric() or their likes directly. > > But trying to follow the advice: > > (this is without package method attached, but the results are the > same with): > > First doing as one shouldn't: > > > table( as.numeric(EdadC) ) > > 1 2 3 4 5 > 20 99 157 127 74 > > Doing as the FAQ says: > > > table( as.numeric(as.character(EdadC)) ) > character(0) > Warning message: > NAs introduced by coercion > > or: > > > table( as.numeric(levels(EdadC))[as.integer(EdadC)] ) > character(0) > Warning message: > NAs introduced by coercion > > Kjetil Halvorsen >
On 21 Aug 2003 at 20:40, kjetil brinchmann halvorsen wrote: After some private responses I did what I should have done at beginning, opened "S Programming" at page 15. It is much clearer than the FAQ, which I think is slighly misleading. Kjetil Halvorsen> Hola! > > The R FAQ says: > > 7.12 How do I convert factors to numeric? > > It may happen that when reading numeric data into R (usually, when > reading in a file), they come in as factors. If f is such a factor > object, you can use > > as.numeric(as.character(f)) > > to get the numbers back. More efficient, but harder to remember, is > > as.numeric(levels(f))[as.integer(f)] > > In any case, do not call as.numeric() or their likes directly. > > But trying to follow the advice: > > (this is without package method attached, but the results are the > same with): > > First doing as one shouldn't: > > > table( as.numeric(EdadC) ) > > 1 2 3 4 5 > 20 99 157 127 74 > > Doing as the FAQ says: > > > table( as.numeric(as.character(EdadC)) ) > character(0) > Warning message: > NAs introduced by coercion > > or: > > > table( as.numeric(levels(EdadC))[as.integer(EdadC)] ) > character(0) > Warning message: > NAs introduced by coercion > > ? > > Kjetil Halvorsen > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help