Skip to content

Instantly share code, notes, and snippets.

@IndrajeetPatil
Created October 24, 2021 15:13
Show Gist options
  • Save IndrajeetPatil/baff8cf625d9fcd5ef2a36f520373426 to your computer and use it in GitHub Desktop.
Save IndrajeetPatil/baff8cf625d9fcd5ef2a36f520373426 to your computer and use it in GitHub Desktop.
Alternative to `case_when` when multiple conditions present
library(dplyr, warn.conflicts = FALSE)
l <- letters[1:3]
n <- 1:3
df <- tibble(x = c(rep(l, 2), "x"))
# thanks to Bret Beheim
df %>% mutate(y = n[match(x, l)])
# thanks to Lisa DeBruine and Brenton Wiernik
df %>% mutate(y = recode(!!!setNames(n, l), x, .default = NA_integer_))
# thanks to Mickaël Canouil
f <- function(.x) if (length(out <- n[l %in% .x]) == 0) NA_integer_ else out
df %>% rowwise() %>% mutate(y = f(x)) %>% ungroup() # option-1
df %>% mutate(y = purrr::map_int(x, f)) # option-2
# thanks to Henrik Singmann
names(n) <- l
df %>% mutate(y = n[x])
@IndrajeetPatil
Copy link
Author

IndrajeetPatil commented Oct 24, 2021

Context:
https://twitter.com/patilindrajeets/status/1452199333827334150?s=20

Reprex:

library(dplyr, warn.conflicts = FALSE)

l <- letters[1:3] 
n <- 1:3
df <- tibble(x = c(rep(l, 2), "x"))

# thanks to Bret Beheim
df %>% mutate(y = n[match(x, l)])
#> # A tibble: 7 x 2
#>   x         y
#>   <chr> <int>
#> 1 a         1
#> 2 b         2
#> 3 c         3
#> 4 a         1
#> 5 b         2
#> 6 c         3
#> 7 x        NA

# thanks to Lisa DeBruine and Brenton Wiernik
df %>% mutate(y = recode(!!!setNames(n, l), x, .default = NA_integer_))
#> # A tibble: 7 x 2
#>   x         y
#>   <chr> <int>
#> 1 a         1
#> 2 b         2
#> 3 c         3
#> 4 a         1
#> 5 b         2
#> 6 c         3
#> 7 x        NA

# thanks to Mickaël Canouil
f <- function(.x) if (length(out <- n[l %in% .x]) == 0) NA_integer_ else out
df %>% rowwise() %>% mutate(y = f(x)) %>% ungroup() # option-1
#> # A tibble: 7 x 2
#>   x         y
#>   <chr> <int>
#> 1 a         1
#> 2 b         2
#> 3 c         3
#> 4 a         1
#> 5 b         2
#> 6 c         3
#> 7 x        NA
df %>% mutate(y = purrr::map_int(x, f))             # option-2
#> # A tibble: 7 x 2
#>   x         y
#>   <chr> <int>
#> 1 a         1
#> 2 b         2
#> 3 c         3
#> 4 a         1
#> 5 b         2
#> 6 c         3
#> 7 x        NA

# thanks to Henrik Singmann
names(n) <- l
df %>% mutate(y = n[x])
#> # A tibble: 7 x 2
#>   x         y
#>   <chr> <int>
#> 1 a         1
#> 2 b         2
#> 3 c         3
#> 4 a         1
#> 5 b         2
#> 6 c         3
#> 7 x        NA

Created on 2021-10-24 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> - Session info  --------------------------------------------------------------
#>  hash: love-you gesture: medium-dark skin tone, sponge, family
#> 
#>  setting  value
#>  version  R version 4.1.1 (2021-08-10)
#>  os       Windows 10 x64 (build 19043)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_United Kingdom.1252
#>  ctype    English_United Kingdom.1252
#>  tz       Europe/Berlin
#>  date     2021-10-24
#>  pandoc   2.14.2 @ C:/PROGRA~1/Pandoc/ (via rmarkdown)
#> 
#> - Packages -------------------------------------------------------------------
#>  package     * version     date (UTC) lib source
#>  assertthat    0.2.1       2019-03-21 [1] CRAN (R 4.1.1)
#>  backports     1.2.1       2020-12-09 [1] CRAN (R 4.1.0)
#>  cli           3.0.1       2021-07-17 [1] CRAN (R 4.1.0)
#>  crayon        1.4.1       2021-02-08 [1] CRAN (R 4.1.1)
#>  DBI           1.1.1       2021-01-15 [1] CRAN (R 4.1.1)
#>  digest        0.6.28      2021-09-23 [1] CRAN (R 4.1.1)
#>  dplyr       * 1.0.7       2021-06-18 [1] CRAN (R 4.1.0)
#>  ellipsis      0.3.2       2021-04-29 [1] CRAN (R 4.1.0)
#>  evaluate      0.14        2019-05-28 [1] CRAN (R 4.1.1)
#>  fansi         0.5.0       2021-05-25 [1] CRAN (R 4.1.1)
#>  fastmap       1.1.0       2021-01-25 [1] CRAN (R 4.1.1)
#>  fs            1.5.0       2020-07-31 [1] CRAN (R 4.1.1)
#>  generics      0.1.0       2020-10-31 [1] CRAN (R 4.1.1)
#>  glue          1.4.2       2020-08-27 [1] CRAN (R 4.1.1)
#>  highr         0.9         2021-04-16 [1] CRAN (R 4.1.1)
#>  htmltools     0.5.2       2021-08-25 [1] CRAN (R 4.1.1)
#>  knitr         1.36.7      2021-10-20 [1] Github (yihui/knitr@8c6ba6c)
#>  lifecycle     1.0.1       2021-09-24 [1] CRAN (R 4.1.1)
#>  magrittr      2.0.1       2020-11-17 [1] CRAN (R 4.1.1)
#>  pillar        1.6.4       2021-10-18 [1] CRAN (R 4.1.1)
#>  pkgconfig     2.0.3       2019-09-22 [1] CRAN (R 4.1.1)
#>  purrr         0.3.4       2020-04-17 [1] CRAN (R 4.1.1)
#>  R.cache       0.15.0      2021-04-30 [1] CRAN (R 4.1.1)
#>  R.methodsS3   1.8.1       2020-08-26 [1] CRAN (R 4.1.0)
#>  R.oo          1.24.0      2020-08-26 [1] CRAN (R 4.1.0)
#>  R.utils       2.11.0      2021-09-26 [1] CRAN (R 4.1.1)
#>  R6            2.5.1       2021-08-19 [1] CRAN (R 4.1.1)
#>  reprex        2.0.1       2021-08-05 [1] CRAN (R 4.1.1)
#>  rlang         0.99.0.9000 2021-10-21 [1] Github (r-lib/rlang@9200c00)
#>  rmarkdown     2.11.3      2021-10-09 [1] Github (rstudio/rmarkdown@5a3e941)
#>  rstudioapi    0.13        2020-11-12 [1] CRAN (R 4.1.1)
#>  sessioninfo   1.1.1.9000  2021-10-13 [1] Github (r-lib/sessioninfo@43a85d5)
#>  stringi       1.7.5       2021-10-04 [1] CRAN (R 4.1.1)
#>  stringr       1.4.0       2019-02-10 [1] CRAN (R 4.1.1)
#>  styler        1.6.2.9000  2021-10-20 [1] Github (r-lib/styler@bf559f6)
#>  tibble        3.1.5       2021-09-30 [1] CRAN (R 4.1.1)
#>  tidyselect    1.1.1       2021-04-30 [1] CRAN (R 4.1.1)
#>  utf8          1.2.2       2021-07-24 [1] CRAN (R 4.1.1)
#>  vctrs         0.3.8       2021-04-29 [1] CRAN (R 4.1.1)
#>  withr         2.4.2       2021-04-18 [1] CRAN (R 4.1.1)
#>  xfun          0.27        2021-10-18 [1] CRAN (R 4.1.1)
#>  yaml          2.2.1       2020-02-01 [1] CRAN (R 4.1.0)
#> 
#>  [1] C:/Users/IndrajeetPatil/Documents/R/win-library/4.1
#>  [2] C:/Program Files/R/R-4.1.1/library
#> 
#> ------------------------------------------------------------------------------

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment