Search code examples
rdplyrcoercion

How do I correctly handle NA coercion warning


I want to cast a column from character to numeric. Some values cannot be cast. This results in a warning, which is expected behavior.

data.frame(a=c("5","7","not_a_number"),stringsAsFactors = F) %>% mutate(value=as.numeric(a))

Additionally I have another column giving me the information, which rows can be cast to numeric (logical). I want to use this column so that R can be sure that it doesn't have to coerce.

data.frame(a=c("5","7","not_a_number"),b=c(1,1,0),stringsAsFactors = F) %>% 
mutate(value=ifelse(b,as.numeric(a),NA_integer_))

But this gives the same error. Why? Nothing should be coerced here. I am handling and responsible for the correct and compatible type across the rows. What is happening?


Solution

  • You need to apply as.numeric outside of ifelse :

    library(dplyr)
    df %>% mutate(value = as.numeric(ifelse(b,a,NA)))
    
    #             a b value
    #1            5 1     5
    #2            7 1     7
    #3 not_a_number 0  <NA>
    

    where df is :

    df <- data.frame(a=c("5","7","not_a_number"),b=c(1,1,0),stringsAsFactors = FALSE)