Skip to content

Instantly share code, notes, and snippets.

@njtierney
Last active July 18, 2025 05:38
Show Gist options
  • Save njtierney/863b63e233431e0c645ef9c7abddeb65 to your computer and use it in GitHub Desktop.
Save njtierney/863b63e233431e0c645ef9c7abddeb65 to your computer and use it in GitHub Desktop.
library(tidyverse)
rule_data <- tibble(
  a_response = c(
    "1, 1 or 2 | 2, 3 or 4",
    "1, 1 to 5 months | 2, 6 months to a year",
    "1, 5 minutes or less | 2, 6-30 minutes"
  ),
  rule_single_response = c(FALSE, FALSE, FALSE),
  output = c(
    min(c(NA, 10, 1), na.rm = TRUE), 
    max(c(NA, 10, 1), na.rm = TRUE), 
    NA
  )
)

rule_data 
#> # A tibble: 3 × 3
#>   a_response                               rule_single_response output
#>   <chr>                                    <lgl>                 <dbl>
#> 1 1, 1 or 2 | 2, 3 or 4                    FALSE                     1
#> 2 1, 1 to 5 months | 2, 6 months to a year FALSE                    10
#> 3 1, 5 minutes or less | 2, 6-30 minutes   FALSE                    NA
example_data <- tibble(
    a_response = c(
    rep("1, 1 or 2 | 2, 3 or 4", 4),
    rep("1, 1 to 5 months | 2, 6 months to a year",3),
    rep("1, 5 minutes or less | 2, 6-30 minutes", 2)
  ),
  rule_single_response = c(
    rep(FALSE, 9)
  )
  )

example_data
#> # A tibble: 9 × 2
#>   a_response                               rule_single_response
#>   <chr>                                    <lgl>               
#> 1 1, 1 or 2 | 2, 3 or 4                    FALSE               
#> 2 1, 1 or 2 | 2, 3 or 4                    FALSE               
#> 3 1, 1 or 2 | 2, 3 or 4                    FALSE               
#> 4 1, 1 or 2 | 2, 3 or 4                    FALSE               
#> 5 1, 1 to 5 months | 2, 6 months to a year FALSE               
#> 6 1, 1 to 5 months | 2, 6 months to a year FALSE               
#> 7 1, 1 to 5 months | 2, 6 months to a year FALSE               
#> 8 1, 5 minutes or less | 2, 6-30 minutes   FALSE               
#> 9 1, 5 minutes or less | 2, 6-30 minutes   FALSE

left_join(rule_data, example_data, by = c("a_response", "rule_single_response"))
#> # A tibble: 9 × 3
#>   a_response                               rule_single_response output
#>   <chr>                                    <lgl>                 <dbl>
#> 1 1, 1 or 2 | 2, 3 or 4                    FALSE                     1
#> 2 1, 1 or 2 | 2, 3 or 4                    FALSE                     1
#> 3 1, 1 or 2 | 2, 3 or 4                    FALSE                     1
#> 4 1, 1 or 2 | 2, 3 or 4                    FALSE                     1
#> 5 1, 1 to 5 months | 2, 6 months to a year FALSE                    10
#> 6 1, 1 to 5 months | 2, 6 months to a year FALSE                    10
#> 7 1, 1 to 5 months | 2, 6 months to a year FALSE                    10
#> 8 1, 5 minutes or less | 2, 6-30 minutes   FALSE                    NA
#> 9 1, 5 minutes or less | 2, 6-30 minutes   FALSE                    NA

Created on 2025-07-09 with reprex v2.1.1

Session info

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.5.1 (2025-06-13)
#>  os       macOS Sonoma 14.5
#>  system   aarch64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_AU.UTF-8
#>  ctype    en_AU.UTF-8
#>  tz       Australia/Hobart
#>  date     2025-07-09
#>  pandoc   3.6.3 @ /Applications/Positron.app/Contents/Resources/app/quarto/bin/tools/aarch64/ (via rmarkdown)
#>  quarto   1.7.31 @ /usr/local/bin/quarto
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package      * version    date (UTC) lib source
#>  cli            3.6.5      2025-04-23 [1] CRAN (R 4.5.0)
#>  digest         0.6.37     2024-08-19 [1] CRAN (R 4.5.0)
#>  dplyr        * 1.1.4      2023-11-17 [1] CRAN (R 4.5.0)
#>  evaluate       1.0.4      2025-06-18 [1] CRAN (R 4.5.0)
#>  farver         2.1.2      2024-05-13 [1] CRAN (R 4.5.0)
#>  fastmap        1.2.0      2024-05-15 [1] CRAN (R 4.5.0)
#>  forcats      * 1.0.0      2023-01-29 [1] CRAN (R 4.5.0)
#>  fs             1.6.6      2025-04-12 [1] CRAN (R 4.5.0)
#>  generics       0.1.4      2025-05-09 [1] CRAN (R 4.5.0)
#>  ggplot2      * 3.5.2      2025-04-09 [1] CRAN (R 4.5.0)
#>  glue           1.8.0      2024-09-30 [1] CRAN (R 4.5.0)
#>  gtable         0.3.6      2024-10-25 [1] CRAN (R 4.5.0)
#>  hms            1.1.3      2023-03-21 [1] CRAN (R 4.5.0)
#>  htmltools      0.5.8.1    2024-04-04 [1] CRAN (R 4.5.0)
#>  knitr          1.50       2025-03-16 [1] CRAN (R 4.5.0)
#>  lifecycle      1.0.4      2023-11-07 [1] CRAN (R 4.5.0)
#>  lubridate    * 1.9.4      2024-12-08 [1] CRAN (R 4.5.0)
#>  magrittr       2.0.3      2022-03-30 [1] CRAN (R 4.5.0)
#>  pillar         1.11.0     2025-07-04 [1] CRAN (R 4.5.0)
#>  pkgconfig      2.0.3      2019-09-22 [1] CRAN (R 4.5.0)
#>  purrr        * 1.0.4.9000 2025-05-04 [1] Github (tidyverse/purrr@9c8beb4)
#>  R6             2.6.1      2025-02-15 [1] CRAN (R 4.5.0)
#>  RColorBrewer   1.1-3      2022-04-03 [1] CRAN (R 4.5.0)
#>  readr        * 2.1.5      2024-01-10 [1] CRAN (R 4.5.0)
#>  reprex         2.1.1      2024-07-06 [1] CRAN (R 4.5.0)
#>  rlang          1.1.6      2025-04-11 [1] CRAN (R 4.5.0)
#>  rmarkdown      2.29       2024-11-04 [1] CRAN (R 4.5.0)
#>  scales         1.4.0      2025-04-24 [1] CRAN (R 4.5.0)
#>  sessioninfo    1.2.3      2025-02-05 [1] CRAN (R 4.5.0)
#>  stringi        1.8.7      2025-03-27 [1] CRAN (R 4.5.0)
#>  stringr      * 1.5.1      2023-11-14 [1] CRAN (R 4.5.0)
#>  tibble       * 3.3.0      2025-06-08 [1] CRAN (R 4.5.0)
#>  tidyr        * 1.3.1      2024-01-24 [1] CRAN (R 4.5.0)
#>  tidyselect     1.2.1      2024-03-11 [1] CRAN (R 4.5.0)
#>  tidyverse    * 2.0.0      2023-02-22 [1] CRAN (R 4.5.0)
#>  timechange     0.3.0      2024-01-18 [1] CRAN (R 4.5.0)
#>  tzdb           0.5.0      2025-03-15 [1] CRAN (R 4.5.0)
#>  utf8           1.2.6      2025-06-08 [1] CRAN (R 4.5.0)
#>  vctrs          0.6.5      2023-12-01 [1] CRAN (R 4.5.0)
#>  withr          3.0.2      2024-10-28 [1] CRAN (R 4.5.0)
#>  xfun           0.52       2025-04-02 [1] CRAN (R 4.5.0)
#>  yaml           2.3.10     2024-07-26 [1] CRAN (R 4.5.0)
#> 
#>  [1] /Users/nick_1/Library/R/arm64/4.5/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library
#>  * ── Packages attached to the search path.
#> 
#> ──────────────────────────────────────────────────────────────────────────────
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment