user3303314
user3303314

Reputation: 81

Cant convert factor to numeric in R

I've tried:

I always get:

Warning message:
NAs introduced by coercion 
> i
 [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA

This is the data I want to be numeric:

> Impress
 [1]  24,085,563.00   35,962,587.00   31,714,513.00   28,206,422.00   40,161,010.00   36,292,929.00   31,545,482.00 
 [8]  28,213,878.00   35,799,224.00   32,400,885.00   28,496,459.00   37,456,344.00   38,108,667.00   33,407,771.00 
[15]  32,540,479.00   30,692,707.00   22,873,000.00   21,329,146.00   28,921,953.00   30,471,519.00   28,601,289.00 
[22]  27,450,630.00   26,708,790.00   19,825,041.00   18,844,169.00   29,592,039.00   31,012,594.00   28,792,531.00 
[29]  28,578,028.00   24,913,985.00 
30 Levels:  18,844,169.00   19,825,041.00   21,329,146.00   22,873,000.00   24,085,563.00   24,913,985.00  ...  40,161,010.00 

> paste(Impress)
 [1] " 24,085,563.00 " " 35,962,587.00 " " 31,714,513.00 " " 28,206,422.00 " " 40,161,010.00 " " 36,292,929.00 " " 31,545,482.00 "
 [8] " 28,213,878.00 " " 35,799,224.00 " " 32,400,885.00 " " 28,496,459.00 " " 37,456,344.00 " " 38,108,667.00 " " 33,407,771.00 "
[15] " 32,540,479.00 " " 30,692,707.00 " " 22,873,000.00 " " 21,329,146.00 " " 28,921,953.00 " " 30,471,519.00 " " 28,601,289.00 "
[22] " 27,450,630.00 " " 26,708,790.00 " " 19,825,041.00 " " 18,844,169.00 " " 29,592,039.00 " " 31,012,594.00 " " 28,792,531.00 "
[29] " 28,578,028.00 " " 24,913,985.00 "

and when I do i<-as.numeric(Impress), it pastes the wrong values.

Thanks!

Upvotes: 0

Views: 1145

Answers (2)

Christopher Louden
Christopher Louden

Reputation: 7592

Because the data has commas, R cannot convert it to a numeric. You have to remove the commas with sub() first and then convert:

i <- as.numeric(gsub(",", "", as.character(impress)))

Upvotes: 1

Gavin Simpson
Gavin Simpson

Reputation: 174948

As far as the computer is concerned, , is not a number and hence any number string containing it must not be numeric, even if to a human these look like perfectly acceptable numbers.

Get rid of the , and then it will work, e.g. using gsub()

i <- as.numeric(gsub(",", "", as.character(Impress)))

E.g.

Impress <- c("24,085,563.00", "35,962,587.00", "31,714,513.00", "28,206,422.00")
gsub(",", "", as.character(Impress))
i <- as.numeric(gsub(",", "", as.character(Impress)))
i

R> gsub(",", "", as.character(Impress))
[1] "24085563.00" "35962587.00" "31714513.00" "28206422.00"
R> i
[1] 24085563 35962587 31714513 28206422
R> is.numeric(i)
[1] TRUE

Upvotes: 1

Related Questions