Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

case_when setup in R – example code gives errors

I am copying some example code from this page: https://dplyr.tidyverse.org/reference/case_match.html

i am having trouble getting case_when to work. my code is:


library(dplyr)
# `case_when()` is not a tidy eval function. If you'd like to reuse
# the same patterns, extract the `case_when()` call in a normal
# function:
case_character_type <- function(height, mass, species) {
  case_when(
    height > 200 | mass > 200 ~ "large",
    species == "Droid" ~ "robot",
    .default = "other"
  )
}

case_character_type(150, 250, "Droid")
#> [1] "large"

the output i get is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Error in case_when( (scratch_31.R#6): Case 3 (`height > 200 | mass > 200 ~ "large"`) must be a two-sided formula, not a character vector.

i am not parsing in any character vectors, and the code is literally copied from the example page documentation.

What am i doing wrongly?

version
               _                           
platform       x86_64-apple-darwin20.6.0   
arch           x86_64                      
os             darwin20.6.0                
system         x86_64, darwin20.6.0        
status                                     
major          4                           
minor          2.2                         
year           2022                        
month          10                          
day            31                          
svn rev        83211                       
language       R                           
version.string R version 4.2.2 (2022-10-31)
nickname       Innocent and Trusting       

> sessionInfo()
R version 4.2.2 (2022-10-31)
Platform: x86_64-apple-darwin20.6.0 (64-bit)
Running under: macOS Big Sur 11.7.4

Matrix products: default
LAPACK: /opt/local/Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_AU.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_1.0.10


>Solution :

The syntax error in your call to case_when() appears to be the reference to .default. If you want a blanket catch all case, use TRUE as the condition:

case_character_type <- function(height, mass, species) {
    case_when(
        height > 200 | mass > 200 ~ "large",
        species == "Droid" ~ "robot",
        TRUE ~ "other"
    )
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading