dplyr::between function doesn't match x >= left and x <= right

I'm doing an advent of code challenge - not homework. I was trying to use the between function but my solution wasn't correct so I manually used inequalities instead and that worked. Any idea what is going on? In the example below, I expected pass1 and pass2 to be the same!

library(tidyverse)
library(here)
#> here() starts at blah

input_in <- structure(list(X1 = c("13-14", "10-12", "2-3", "2-3", "2-11", 
                                  "1-6", "16-20", "4-5", "10-13", "10-14"), X2 = c("f:", "w:", 
                                                                                   "n:", "h:", "c:", "b:", "q:", "m:", "h:", "x:"), X3 = c("ffffffffnfffvv", 
                                                                                                                                           "kwtzpnzspwwwdz", "nnjn", "hhhh", "crccccccccsccc", "lcpcbcr", 
                                                                                                                                           "qsqqqqqqqjqqqvqqqqqh", "mmfmm", "hhhhhhchhkhhphh", "xxvxxxxxxvxxxwx"
                                                                                   )), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, 
                                                                                                                                                              -10L), spec = structure(list(cols = list(X1 = structure(list(), class = c("collector_character", 
                                                                                                                                                                                                                                        "collector")), X2 = structure(list(), class = c("collector_character", 
                                                                                                                                                                                                                                                                                        "collector")), X3 = structure(list(), class = c("collector_character", 
                                                                                                                                                                                                                                                                                                                                        "collector"))), default = structure(list(), class = c("collector_guess", 
                                                                                                                                                                                                                                                                                                                                                                                              "collector")), skip = 0), class = "col_spec"))

pwd_check <- input_in %>%
  rename(letter=X2, pwd=X3) %>%
  separate(X1, into=c("minCount", "maxCount"), sep="-", convert=TRUE) %>%
  mutate(letter=str_sub(letter, 1, 1), 
         count_letter = str_count(pwd, letter), 
         pass1 = count_letter >= minCount & count_letter <= maxCount,
         pass2 = between(count_letter, minCount, maxCount), 
         row = row_number()
  )

# Expect pass1 to be the same as pass2
pwd_check %>%
  filter(pass1 != pass2)
#> # A tibble: 6 x 8
#>   minCount maxCount letter pwd                  count_letter pass1 pass2   row
#>      <int>    <int> <chr>  <chr>                       <int> <lgl> <lgl> <int>
#> 1        2        3 n      nnjn                            3 TRUE  FALSE     3
#> 2        1        6 b      lcpcbcr                         1 TRUE  FALSE     6
#> 3       16       20 q      qsqqqqqqqjqqqvqqqqqh           16 TRUE  FALSE     7
#> 4        4        5 m      mmfmm                           4 TRUE  FALSE     8
#> 5       10       13 h      hhhhhhchhkhhphh                12 TRUE  FALSE     9
#> 6       10       14 x      xxvxxxxxxvxxxwx                12 TRUE  FALSE    10

Created on 2020-12-02 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> - Session info ---------------------------------------------------------------
#>  setting  value                       
#>  version  R version 4.0.3 (2020-10-10)
#>  os       Windows 10 x64              
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  English_United States.1252  
#>  ctype    English_United States.1252  
#>  tz       America/New_York            
#>  date     2020-12-02                  
#> 
#> - Packages -------------------------------------------------------------------
#>  package     * version date       lib source        
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.0)
#>  backports     1.1.6   2020-04-05 [1] CRAN (R 4.0.0)
#>  blob          1.2.1   2020-01-20 [1] CRAN (R 4.0.0)
#>  broom         0.7.1   2020-10-02 [1] CRAN (R 4.0.2)
#>  callr         3.4.3   2020-03-28 [1] CRAN (R 4.0.0)
#>  cellranger    1.1.0   2016-07-27 [1] CRAN (R 4.0.0)
#>  cli           2.0.2   2020-02-28 [1] CRAN (R 4.0.0)
#>  colorspace    1.4-1   2019-03-18 [1] CRAN (R 4.0.0)
#>  crayon        1.3.4   2017-09-16 [1] CRAN (R 4.0.0)
#>  DBI           1.1.0   2019-12-15 [1] CRAN (R 4.0.0)
#>  dbplyr        1.4.4   2020-05-27 [1] CRAN (R 4.0.3)
#>  desc          1.2.0   2018-05-01 [1] CRAN (R 4.0.0)
#>  devtools      2.3.0   2020-04-10 [1] CRAN (R 4.0.0)
#>  digest        0.6.25  2020-02-23 [1] CRAN (R 4.0.0)
#>  dplyr       * 1.0.2   2020-08-18 [1] CRAN (R 4.0.3)
#>  ellipsis      0.3.1   2020-05-15 [1] CRAN (R 4.0.2)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.0)
#>  fansi         0.4.1   2020-01-08 [1] CRAN (R 4.0.0)
#>  forcats     * 0.5.0   2020-03-01 [1] CRAN (R 4.0.0)
#>  fs            1.4.1   2020-04-04 [1] CRAN (R 4.0.0)
#>  generics      0.0.2   2018-11-29 [1] CRAN (R 4.0.0)
#>  ggplot2     * 3.3.2   2020-06-19 [1] CRAN (R 4.0.3)
#>  glue          1.4.1   2020-05-13 [1] CRAN (R 4.0.2)
#>  gtable        0.3.0   2019-03-25 [1] CRAN (R 4.0.0)
#>  haven         2.3.1   2020-06-01 [1] CRAN (R 4.0.3)
#>  here        * 0.1     2017-05-28 [1] CRAN (R 4.0.2)
#>  highr         0.8     2019-03-20 [1] CRAN (R 4.0.0)
#>  hms           0.5.3   2020-01-08 [1] CRAN (R 4.0.0)
#>  htmltools     0.4.0   2019-10-04 [1] CRAN (R 4.0.0)
#>  httr          1.4.2   2020-07-20 [1] CRAN (R 4.0.2)
#>  jsonlite      1.7.1   2020-09-07 [1] CRAN (R 4.0.3)
#>  knitr         1.28    2020-02-06 [1] CRAN (R 4.0.0)
#>  lifecycle     0.2.0   2020-03-06 [1] CRAN (R 4.0.0)
#>  lubridate     1.7.9   2020-06-08 [1] CRAN (R 4.0.3)
#>  magrittr      1.5     2014-11-22 [1] CRAN (R 4.0.0)
#>  memoise       1.1.0   2017-04-21 [1] CRAN (R 4.0.0)
#>  modelr        0.1.8   2020-05-19 [1] CRAN (R 4.0.3)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.0.0)
#>  pillar        1.4.6   2020-07-10 [1] CRAN (R 4.0.2)
#>  pkgbuild      1.0.6   2019-10-09 [1] CRAN (R 4.0.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.0.0)
#>  pkgload       1.0.2   2018-10-29 [1] CRAN (R 4.0.0)
#>  prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.0.0)
#>  processx      3.4.2   2020-02-09 [1] CRAN (R 4.0.0)
#>  ps            1.3.2   2020-02-13 [1] CRAN (R 4.0.0)
#>  purrr       * 0.3.4   2020-04-17 [1] CRAN (R 4.0.0)
#>  R6            2.4.1   2019-11-12 [1] CRAN (R 4.0.0)
#>  Rcpp          1.0.5   2020-07-06 [1] CRAN (R 4.0.2)
#>  readr       * 1.4.0   2020-10-05 [1] CRAN (R 4.0.3)
#>  readxl        1.3.1   2019-03-13 [1] CRAN (R 4.0.0)
#>  remotes       2.1.1   2020-02-15 [1] CRAN (R 4.0.0)
#>  reprex        0.3.0   2019-05-16 [1] CRAN (R 4.0.0)
#>  rlang         0.4.8   2020-10-08 [1] CRAN (R 4.0.3)
#>  rmarkdown     2.3     2020-06-18 [1] CRAN (R 4.0.2)
#>  rprojroot     1.3-2   2018-01-03 [1] CRAN (R 4.0.0)
#>  rvest         0.3.6   2020-07-25 [1] CRAN (R 4.0.2)
#>  scales        1.1.0   2019-11-18 [1] CRAN (R 4.0.0)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.0)
#>  stringi       1.4.6   2020-02-17 [1] CRAN (R 4.0.0)
#>  stringr     * 1.4.0   2019-02-10 [1] CRAN (R 4.0.0)
#>  testthat      2.3.2   2020-03-02 [1] CRAN (R 4.0.0)
#>  tibble      * 3.0.4   2020-10-12 [1] CRAN (R 4.0.3)
#>  tidyr       * 1.1.2   2020-08-27 [1] CRAN (R 4.0.3)
#>  tidyselect    1.1.0   2020-05-11 [1] CRAN (R 4.0.2)
#>  tidyverse   * 1.3.0   2019-11-21 [1] CRAN (R 4.0.2)
#>  usethis       1.6.1   2020-04-29 [1] CRAN (R 4.0.2)
#>  utf8          1.1.4   2018-05-24 [1] CRAN (R 4.0.0)
#>  vctrs         0.3.2   2020-07-15 [1] CRAN (R 4.0.2)
#>  withr         2.2.0   2020-04-20 [1] CRAN (R 4.0.0)
#>  xfun          0.13    2020-04-13 [1] CRAN (R 4.0.0)
#>  xml2          1.3.2   2020-04-23 [1] CRAN (R 4.0.0)
#>  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.0)
#> 
#> [1] C:/Users/sazimmer/Documents/R/win-library/4.0
#> [2] C:/Users/sazimmer/Documents/R/R-4.0.3/library
2 Likes

Oh... between is not vectorized! See https://github.com/tidyverse/dplyr/issues/5493

Closing this issue but leaving up for posterity in case anyone else searches.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.