rna

Warning message: NAs introduced by coercion while changing to numeric


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


Solution

  • 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"))