README.md

incase

License:
MIT R build
status CodeFactor Dependencies

incase provides a more pipe-friendly alternative to dplyr’s case_when() and if_else().

Installation

You can install the released version of incase from CRAN with:

install.packages("incase")

or the development version from GitHub with:

# install.packages("remotes")
remotes::install_github("rossellhayes/incase")

Usage

Pipe-friendly conditionals

incase’s in_case() and if_case() accept a vector as their first input, allowing you to take full advantage of magrittr’s .

1:20 %>%
  in_case(
    . %% 15 == 0 ~ "fizz buzz",
    . %%  3 == 0 ~ "fizz",
    . %%  5 == 0 ~ "buzz",
    TRUE         ~ .
  )
#>  [1] "1"         "2"         "fizz"      "4"         "buzz"      "fizz"     
#>  [7] "7"         "8"         "fizz"      "buzz"      "11"        "fizz"     
#> [13] "13"        "14"        "fizz buzz" "16"        "17"        "fizz"     
#> [19] "19"        "buzz"

1:20 %>% if_case(. %% 3 == 0, "fizz", .)
#>  [1] "1"    "2"    "fizz" "4"    "5"    "fizz" "7"    "8"    "fizz" "10"  
#> [11] "11"   "fizz" "13"   "14"   "fizz" "16"   "17"   "fizz" "19"   "20"

Automatic type conversion

incase functions automatically coerce types. This is especially useful when dealing with integers or NAs.

x <- -1:5

# Replace -1 with NA
dplyr::case_when(x == -1 ~ NA, TRUE ~ x)
#> Error: must be a logical vector, not an integer vector.
dplyr::case_when(x == -1 ~ NA_integer_, TRUE ~ x)
#> [1] NA  0  1  2  3  4  5
in_case(x == -1 ~ NA, TRUE ~ x)
#> [1] NA  0  1  2  3  4  5

# Replace -1 with 0
dplyr::case_when(x == -1 ~ 0, TRUE ~ x)
#> Error: must be a double vector, not an integer vector.
dplyr::case_when(x == -1 ~ 0L, TRUE ~ x)
#> [1] 0 0 1 2 3 4 5
in_case(x == -1 ~ 0, TRUE ~ x)
#> [1] 0 0 1 2 3 4 5

With incase, you no longer have to worry about specifying the type of your NAs or adding L to your integers.

Easy default values

in_case() adds preserve and default arguments as a more intuitive alternative to TRUE ~ ....*

1:20 %>%
  in_case(
    . %% 15 == 0 ~ "fizz buzz",
    . %%  3 == 0 ~ "fizz",
    . %%  5 == 0 ~ "buzz"
  )
#>  [1] NA          NA          "fizz"      NA          "buzz"      "fizz"     
#>  [7] NA          NA          "fizz"      "buzz"      NA          "fizz"     
#> [13] NA          NA          "fizz buzz" NA          NA          "fizz"     
#> [19] NA          "buzz"

1:20 %>%
  in_case(
    . %% 15 == 0 ~ "fizz buzz",
    . %%  3 == 0 ~ "fizz",
    . %%  5 == 0 ~ "buzz",
    preserve     = TRUE
  )
#>  [1] "1"         "2"         "fizz"      "4"         "buzz"      "fizz"     
#>  [7] "7"         "8"         "fizz"      "buzz"      "11"        "fizz"     
#> [13] "13"        "14"        "fizz buzz" "16"        "17"        "fizz"     
#> [19] "19"        "buzz"

1:20 %>%
  in_case(
    . %% 15 == 0 ~ "fizz buzz",
    . %%  3 == 0 ~ "fizz",
    . %%  5 == 0 ~ "buzz",
    default      = "pass"
  )
#>  [1] "pass"      "pass"      "fizz"      "pass"      "buzz"      "fizz"     
#>  [7] "pass"      "pass"      "fizz"      "buzz"      "pass"      "fizz"     
#> [13] "pass"      "pass"      "fizz buzz" "pass"      "pass"      "fizz"     
#> [19] "pass"      "buzz"

Simplified interface for recoding

switch_case() works as a convenient shorthand for in_case() when recoding discrete values.

parties
#>  [1] "G" "R" "G" "D" "I" "I" "R" "R" "I" NA  "I" "L" "L" "I" "L" "R" "I" "R" "R"
#> [20] "D"

parties %>% 
  in_case(
    . == "D"           ~ "Democratic",
    . == "R"           ~ "Republican",
    . %in% c("G", "L") ~ "Other",
    . %in% c("I", NA)  ~ "Independent" 
  )
#>  [1] "Other"       "Republican"  "Other"       "Democratic"  "Independent"
#>  [6] "Independent" "Republican"  "Republican"  "Independent" "Independent"
#> [11] "Independent" "Other"       "Other"       "Independent" "Other"      
#> [16] "Republican"  "Independent" "Republican"  "Republican"  "Democratic"

parties %>%
  switch_case(
    "D"         ~ "Democrat",
    "R"         ~ "Republican",
    c("G", "L") ~ "Other",
    c("I", NA)  ~ "Independent"
  )
#>  [1] "Other"       "Republican"  "Other"       "Democrat"    "Independent"
#>  [6] "Independent" "Republican"  "Republican"  "Independent" "Independent"
#> [11] "Independent" "Other"       "Other"       "Independent" "Other"      
#> [16] "Republican"  "Independent" "Republican"  "Republican"  "Democrat"

grep_case() allows you to recode values with pattern matching.

countries <- c(
  "France", "Ostdeutschland", "Westdeutschland", "Nederland",
  "België (Vlaanderen)", "Belgique (Wallonie)", "Luxembourg", "Italia"
)

grep_case(
  countries,
  "Deutschland" ~ "Germany",
  "Belg"        ~ "Belgium",
  "Nederland"   ~ "Netherlands",
  "Italia"      ~ "Italy",
  preserve      = TRUE,
  ignore.case   = TRUE
)
#> [1] "France"      "Germany"     "Germany"     "Netherlands" "Belgium"    
#> [6] "Belgium"     "Luxembourg"  "Italy"

Hex sticker fonts are Source Sans Pro by Adobe and Hasklig by Ian Tuomi.

Please note that incase is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

* Intuitiveness may vary from person to person.



Try the incase package in your browser

Any scripts or data that you put into this service are public.

incase documentation built on Jan. 16, 2021, 5:21 p.m.