Dplyr: case_when not handling is.na statements

Created on 6 Jul 2016  路  4Comments  路  Source: tidyverse/dplyr

case_when doesn't seem to work on is.na statements unless they are the very first formula. I'm submitting a pull request.

library(dplyr)

x <- c(1:6, NA)

case_when(
  is.na(x) ~ "missing",
  TRUE ~ as.character(x)
)
#> [1] "1"       "2"       "3"       "4"       "5"       "6"       "missing"

case_when(
  is.na(x) ~ "missing",
  x %% 2 == 0 ~ "even",
  TRUE ~ as.character(x)
)
#> [1] "1"       "even"    "3"       "even"    "5"       "even"    "missing"

# NA is not recoded
case_when(
  x %% 2 == 0 ~ "even",
  is.na(x) ~ "missing",
  TRUE ~ as.character(x)
)
#> [1] "1"    "even" "3"    "even" "5"    "even" NA

Session info

devtools::session_info()
#> Session info --------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.3.1 (2016-06-21)
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  English_United States.1252  
#>  tz       America/Chicago             
#>  date     2016-07-06
#> Packages ------------------------------------------------------------------
#>  package    * version date       source        
#>  assertthat   0.1     2013-12-06 CRAN (R 3.0.2)
#>  DBI          0.4-1   2016-05-08 CRAN (R 3.2.5)
#>  devtools     1.12.0  2016-06-24 CRAN (R 3.3.1)
#>  digest       0.6.9   2016-01-08 CRAN (R 3.2.2)
#>  dplyr      * 0.5.0   2016-06-24 CRAN (R 3.3.1)
#>  evaluate     0.9     2016-04-29 CRAN (R 3.2.5)
#>  formatR      1.4     2016-05-09 CRAN (R 3.2.3)
#>  htmltools    0.3.5   2016-03-21 CRAN (R 3.2.4)
#>  knitr        1.13    2016-05-09 CRAN (R 3.2.3)
#>  magrittr     1.5     2014-11-22 CRAN (R 3.1.2)
#>  memoise      1.0.0   2016-01-29 CRAN (R 3.2.3)
#>  R6           2.1.2   2016-01-26 CRAN (R 3.2.3)
#>  Rcpp         0.12.5  2016-05-14 CRAN (R 3.2.5)
#>  rmarkdown    0.9.6   2016-05-01 CRAN (R 3.2.3)
#>  stringi      1.1.1   2016-05-27 CRAN (R 3.2.5)
#>  stringr      1.0.0   2015-04-30 CRAN (R 3.2.0)
#>  tibble       1.1     2016-07-04 CRAN (R 3.3.1)
#>  withr        1.0.2   2016-06-20 CRAN (R 3.3.1)
#>  yaml         2.1.13  2014-06-12 CRAN (R 3.1.0)

Most helpful comment

How did you learn about the <details> thing?

All 4 comments

How did you learn about the <details> thing?

@hadley: PR has been merged, NEWS has been added.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings