How to convert a coefficient into a numeric to R without NA entered by a coercion warning message

I have data containing a factor class, so when I convert it to a number class, I get this warning. the following code I wrote in R to convert the coefficient to numeric

class(usedcars$Price)
[1] "factor"

e <- paste(usedcars$Price)
e <- as.numeric(paste(usedcars$Price))
Warning message:
NAs introduced by coercion 

Guys all data is converted to "NA", but the class is numeric. Can someone help me get rid of this NA warning message when converting a coefficient to a numeric value in R?

+4
source share
2 answers

I will try to reproduce your problem:

set.seed(1)
a <- factor(sample(1:100, 10))
> a
 [1] 27 37 57 89 20 86 97 62 58 6 
Levels: 6 20 27 37 57 58 62 86 89 97

Alexwhan's comment is actually good:

> as.numeric(as.character(a))
 [1] 27 37 57 89 20 86 97 62 58  6

Even if your data should be trim()ed, it will still work:

> paste( " ", a, " ")
 [1] "  27  " "  37  " "  57  " "  89  " "  20  " "  86  " "  97  " "  62  " "  58  " "  6  " 
> as.numeric(paste( " ", a, " "))
 [1] 27 37 57 89 20 86 97 62 58  6

SO , ()

> as.numeric(paste(a, "a"))
 [1] NA NA NA NA NA NA NA NA NA NA
Warning message:
NAs introduced by coercion 

, :

> paste( intToUtf8(160), a, intToUtf8(160))
 [1] "  27  " "  37  " "  57  " "  89  " "  20  " "  86  " "  97  " "  62  " "  58  " "  6  " 
> as.numeric(paste( intToUtf8(160), a, intToUtf8(160)))
 [1] NA NA NA NA NA NA NA NA NA NA

intToUtf8 (32) (, ), 160 , , as.numeric ( trim gdata)   NA.

+3

, as.numeric .

, "," (, 1285) "" db <- gsub(",","",db), as.numeric(db)

+3

All Articles