Skip to content

case_when Error: when having only one condition and the vector length is greater than 1 #2909

@AndreMikulec

Description

@AndreMikulec

case_when errors when having only one condition and the vector length is greater than 1

library(dplyr)

x <- 1
dplyr::case_when(
         TRUE ~   x
)
[1] 1

x <- 1:2
dplyr::case_when(
         TRUE ~   x
)
Error: RHS of case 1 (x) must be length 1 (the first output), not 2

But the error seems deceptive. Maybe the left hand side needs a vector the same length as 'x'.
If so, TRUE ( a single ) value, can not be allowed ( or allowed to be a default. ) Alternately, a single value (TRUE) can only be allowed as the default and the case must have at least 2 conditions. If so, this 'feature' should be in the 'help documentation.'

x <- 1:2
dplyr::case_when(
       x == x ~ x
)
[1] 1 2

x <- 1:2
dplyr::case_when(
       TRUE ~ x
)
Error: RHS of case 1 (x) must be length 1 (the first output), not 2

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