Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a character vector to numeric?

I have this character vector but I need to convert this to numeric.

>iono_test_y
[1] "g" "b" "b" "g" "g" "g" "b" "g" "b" "b" "g" "b" "g" "b" "b" "b" "g" "g" "b" "b" "b" "g" "g"
[24] "b" "b" "g" "g" "g" "b" "g" "g" "g" "b" "b" "b" "b" "b" "g" "g" "g" "g" "g" "b" "g" "b" "b"
[47] "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g"
[70] "g" "g"

I already tried

iono_test_y <- as.numeric(as.character(iono_test_y)

but it doesn't seem to work. How can I do that?

like image 914
Bruno Camarda Avatar asked Jan 16 '18 01:01

Bruno Camarda


1 Answers

It appears that your variable is just a character vector, not a factor. You can give it a numeric encoding by using

as.numeric(factor(iono_test_y))
like image 112
G5W Avatar answered Oct 06 '22 00:10

G5W