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?