Skip to content

Commit

Permalink
add example in case_when about consistent RHS (tidyverse#3247)
Browse files Browse the repository at this point in the history
* add example in case_when about consistent RHS

* add dontrun example

* generation of Rd file
  • Loading branch information
cderv authored and krlmlr committed Dec 13, 2017
1 parent fc66342 commit 6e84349
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
25 changes: 25 additions & 0 deletions R/case_when.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,31 @@
#' x %% 35 == 0 ~ "fizz buzz"
#' )
#'
#' # All RHS needs to be the same type. Inconsitent types will throw an error.
#' # This applies also to NA values used in RHS - NA is logical so you must use other
#' # typed value like NA_real_, NA_complex, NA_character_, NA_integer_ in other cases
#' case_when(
#' x %% 35 == 0 ~ NA_character_,
#' x %% 5 == 0 ~ "fizz",
#' x %% 7 == 0 ~ "buzz",
#' TRUE ~ as.character(x)
#' )
#' case_when(
#' x %% 35 == 0 ~ 35,
#' x %% 5 == 0 ~ 5,
#' x %% 7 == 0 ~ 7,
#' TRUE ~ NA_real_
#' )
#' # This throws an error as NA is logical not numeric
#' \dontrun{
#' case_when(
#' x %% 35 == 0 ~ 35,
#' x %% 5 == 0 ~ 5,
#' x %% 7 == 0 ~ 7,
#' TRUE ~ NA
#' )
#' }
#'
#' # case_when is particularly useful inside mutate when you want to
#' # create a new variable that relies on a complex combination of existing
#' # variables
Expand Down
25 changes: 25 additions & 0 deletions man/case_when.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6e84349

Please sign in to comment.