My data looks like this:
df <- expand.grid(grade = c("A", 1, "B", 2, 3, "C"), value = c(0, 1))
When I try to replace the A
, B
, and C
s in a case_when
statement, the non-replaced values go to NA
... I want them to stay!
Attempt:
library(dplyr)
df_new <- df %>%
mutate(grade = case_when(
.$grade == "A" ~ 1,
.$grade == "B" ~ 2,
.$grade == "C" ~ 3))
Desired output:
df <- data.frame(grade = c(rep(1, 4), rep(2, 4), rep(3, 4)), value = rep(c(0, 1), 6))
Note** Looking for case_when
solution NOT ifelse
solution.
case_when
statements are two-sided formulas where the left-hand side is a logical test and the right-hand side is the value to assign when that test is TRUE
.
Values that are never matched by a logical test get a default replacement value: NA
. To keep these values from getting NA
s, include a final catch-all test and replacement.
library(tidyverse)
df <- expand.grid(grade = c("A", 1, "B", 2, 3, "C"), value = c(0, 1))
df %>%
mutate(old_grade = grade) %>%
select(old_grade, grade, value) %>%
mutate(grade = case_when(
grade == "A" ~ 1,
grade == "B" ~ 2,
grade == "C" ~ 3,
TRUE ~ as.numeric(as.character(grade)))
)
#> Warning: Problem with `mutate()` input `grade`.
#> i NAs introduced by coercion
#> i Input `grade` is `case_when(...)`.
#> old_grade grade value
#> 1 A 1 0
#> 2 1 1 0
#> 3 B 2 0
#> 4 2 2 0
#> 5 3 3 0
#> 6 C 3 0
#> 7 A 1 1
#> 8 1 1 1
#> 9 B 2 1
#> 10 2 2 1
#> 11 3 3 1
#> 12 C 3 1
Created on 2021-02-19 by the reprex package (v1.0.0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With