Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with as.numeric in R? [duplicate]

Tags:

r

> X864291X8X74
[1] 8.0000000000  9.0000000000  10.0000000000 6.0000000000  8.0000000000 
10 Levels: 0.0000000000 10.0000000000 12.0000000000 3.0000000000 4.0000000000 6.0000000000 ... NULL

> as.numeric(X864291X8X74)

[1] 8 9 2 6 8

what did I misunterstood? shouldn't be the result of as.numeric 8 9 10 6 8?

How do I get the correct result?

like image 962
teGuy Avatar asked Feb 21 '13 22:02

teGuy


People also ask

What does as numeric do in R?

as. numeric attempts to coerce its argument to numeric type (either integer or real). is. numeric returns TRUE if its argument is of type real or type integer and FALSE otherwise.

What is the difference between numeric and double in R?

It is a historical anomaly that R has two names for its floating-point vectors, double and numeric (and formerly had real ). double is the name of the type. numeric is the name of the mode and also of the implicit class. As an S4 formal class, use "numeric" .

How do I convert numeric to double in R?

To convert an integer to double in R, use the as. double() method. The as. double() is an inbuilt R function that converts an integer to the double class.

How do you set a variable as numeric in R?

There are two steps for converting factor to numeric: Step 1: Convert the data vector into a factor. The factor() command is used to create and modify factors in R. Step 2: The factor is converted into a numeric vector using as. numeric().


1 Answers

Your vector is a factor. This question has been asked quite a few times, ex: here, here, here. In order to convert a factor to numeric, you'll have to convert to character first. Try:

as.numeric(as.character(my_vec))
like image 85
Arun Avatar answered Sep 30 '22 17:09

Arun