Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep value if not in case_when statement

Tags:

r

dplyr

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 Cs 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.

like image 703
emehex Avatar asked Aug 31 '16 19:08

emehex


1 Answers

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 NAs, 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)

like image 109
TJ Mahr Avatar answered Nov 17 '22 02:11

TJ Mahr