Reputation: 153
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?
Upvotes: 0
Views: 275
Reputation: 56189
Indirect answer, why not remove all non-digits before converting to a numeric:
data.frame(a = c("5","7","not_a_number"), stringsAsFactors = FALSE) %>%
mutate(value = as.numeric(gsub("\\D+", "", a)))
# a value
# 1 5 5
# 2 7 7
# 3 not_a_number NA
Upvotes: 0
Reputation: 389047
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)
Upvotes: 2