How to Avoid Warning When Introducing Nas by Coercion

How to avoid warning when introducing NAs by coercion

Use suppressWarnings():

suppressWarnings(as.numeric(c("1", "2", "X")))
[1] 1 2 NA

This suppresses warnings.

Warning message: NAs introduced by coercion while changing to numeric

We can use grep to subset

df$a[grep('^\\d+$', df$a, invert = TRUE)]
#[1] "a"

data

df <- data.frame(a = c("23", "34", "a", "56"))

why do i get NAs introduced by coercion warning message?

As far as I know, yes and no do not equate to 0 and 1 in R. It would work with TRUE and FALSE however. You need to assign a value to "yes" and "no" directly.

cust.df$email<-factor(cust.df$email)
cust.df$email<-as.numeric(cust.df$email)

this will assign 1 and 2 to your data, if you want 0 and 1, then you can simply use:

cust.df$email[cust.df$email==2]<-0

as.numeric( 10^3 ) Warning message: NAs introduced by coercion

split the strings at ^ and then coerce each part individually. We have to escape using \\ because ^ is a special regex character.

sapply(strsplit(vec, "\\^"), function(x){
as.numeric(x[1])^as.numeric(x[2])
})
#[1] 1e+02 1e+03 1e+06 NA 1e+09


Related Topics



Leave a reply



Submit