After reading together a bunch of csv's, I have a character column called response_char that is a mix of characters and numbers.
Using case_when, I want to create a new column response_num where I will apply a set of specific conditional rules to convert the character responses to numbers (when trial == "one"), and then, convert the numeric responses to numbers (when trial == "two").
In the reproducible example below, I get Warning in eval_tidy(pair$rhs, env = default_env): NAs introduced by coercion despite no NA being actually introduced.
Any idea about how can I avoid these type of warnings? Is this a bug?
library(dplyr, warn.conflicts = FALSE)
df <- tibble(trial = c("one", "two"), response_char = c(1.1, "No"))
df
#> # A tibble: 2 x 2
#> trial response_char
#> <chr> <chr>
#> 1 one 1.1
#> 2 two No
df %>%
mutate(
response_num =
case_when(
trial == "two" & response_char == "No" ~ 0,
trial == "one" ~ as.numeric(response_char) # This line creates the warning
)
)
#> Warning in eval_tidy(pair$rhs, env = default_env): NAs introduced by coercion
#> # A tibble: 2 x 3
#> trial response_char response_num
#> <chr> <chr> <dbl>
#> 1 one 1.1 1.1
#> 2 two No 0
Created on 2021-04-13 by the reprex package (v2.0.0)