Skip to content

case_when formula not working as expected #2927

@gorkang

Description

@gorkang

When using case_when with a formula such as Age == 25 & Brochure == "New", its behavior seems not to be equivalent to filter(Age == 25 & Brochure == "New").

This is not expected (at least I didn't expected it) and can cause problems when there are NAs and we do an arithmetic operation as the replacement value.

Please see a reprex where a minimal case is shown.

library(tidyverse)

Age = c(25, 25, NA)
Brochure = c("New", "Old", "New")
PPV_Cond1 = c(NA, 1, NA)
PPV_Cond2 = c(1, NA, 1)
DF = data.frame(Age, Brochure, PPV_Cond1, PPV_Cond2)
DF
#>   Age Brochure PPV_Cond1 PPV_Cond2
#> 1  25      New        NA         1
#> 2  25      Old         1        NA
#> 3  NA      New        NA         1

# One case with Brochure == "New" have NA in Age. If we filter using Age == 25 & Brochure == "New",
# that row disappears as expected
DF %>% filter(Age == 25 & Brochure == "New")
#>   Age Brochure PPV_Cond1 PPV_Cond2
#> 1  25      New        NA         1

# Despite using the same "filter" here, the NA causes problems
DF %>% mutate(Variable =
                case_when(Age == 25 & Brochure == "Old" ~ PPV_Cond1 - 50,
                          Age == 25 & Brochure == "New" ~ PPV_Cond2 - 50))
#> Error in mutate_impl(.data, dots): Evaluation error: NAs are not allowed in subscripted assignments.

# If we add the !is.na(Age) to the case_when condition, everything works as expected
DF %>% mutate(Variable =
                case_when(Age == 25 & Brochure == "Old" ~ PPV_Cond1 - 50,
                     !is.na(Age) & Age == 25 & Brochure == "New" ~ PPV_Cond2 - 50))
#>   Age Brochure PPV_Cond1 PPV_Cond2 Variable
#> 1  25      New        NA         1      -49
#> 2  25      Old         1        NA      -49
#> 3  NA      New        NA         1       NA

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions