1

I am reding CSV where one columns is numeric with commas. I am using following option while reading Prv_mnth_so <- read.csv("sales office previous month.csv", stringsAsFactors = FALSE) then replacing commas with blank Prv_mnth_so1$Lc_Amount <- gsub(",", "", Prv_mnth_so1$Lc_Amount) chanfing it to numeric Prv_mnth_so3$Lc_Amount <- as.numeric(as.character(Prv_mnth_so3$Lc_Amount)) but I get following warning Warning message: NAs introduced by coercion which is creating problem further in doing summary on this column

Ashish
  • 115
  • 3
  • 15
  • 1
    Does any of these three posts solve your problem? [1](https://stackoverflow.com/questions/14984989/how-to-avoid-warning-when-introducing-nas-by-coercion?rq=1), [2](https://stackoverflow.com/questions/17598020/converting-character-to-numeric-without-na-coercion-in-r?rq=1), [3](https://stackoverflow.com/questions/47247640/changing-character-to-numeric-in-data-frame-as-numeric-with-thousand-separator?rq=1) – ekoam Oct 29 '20 at 06:48

2 Answers2

0

You haven't provided an example to give you the exact answer for your data but this warning means there are some values in your data which cannot be converted to numeric.

If you have a very big data and can't manually look into your data what you could do is create a new column with numeric values and then check for values which were turned to NA. For example,

df <- data.frame(a = c("23", "34", "a", "56"))
df$b <- as.numeric(df$a)
#Warning message:
#NAs introduced by coercion 
df$a[is.na(df$b)]
#[1] "a"

You can then decide what you need to do with these values.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

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"))
akrun
  • 874,273
  • 37
  • 540
  • 662