user63230
user63230

Reputation: 4698

dplyr::between issue dplyr 1.0.7

I have opened an old script and the following use of between no longer works. A previous question I had asked demonstrates that it used to work here. Unfortunately, I am not sure what version I was using then.

library(lubridate)
library(tidyverse)
df <- data.frame(date1 = c("2011-09-18", "2013-03-06", "2013-08-08"),
                 date2 = c("2012-02-18", "2014-03-06", "2015-02-03"))
df$date1 <- as.Date(parse_date_time(df$date1, "ymd"))
df$date2 <- as.Date(parse_date_time(df$date2, "ymd"))
df
#        date1      date2
# 1 2011-09-18 2012-02-18
# 2 2013-03-06 2014-03-06
# 3 2013-08-08 2015-02-03

df$y_2014 <- if_else(between(2014, year(df$date1), year(df$date2)), 1, 0, as.numeric(NA))
#between(2014, year(df$date1), year(df$date2))

Error: left must be length 1

sessionInfo()
R version 4.0.3 (2020-10-10)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] forcats_0.5.0     stringr_1.4.0     dplyr_1.0.7       purrr_0.3.4       readr_1.4.0       tidyr_1.1.2      
 [7] tibble_3.0.5      ggplot2_3.3.3     tidyverse_1.3.0   lubridate_1.7.9.2

loaded via a namespace (and not attached):
 [1] tinytex_0.29     tidyselect_1.1.0 xfun_0.20        haven_2.3.1      colorspace_2.0-0 vctrs_0.3.8     
 [7] generics_0.1.0   htmltools_0.5.1  yaml_2.2.1       utf8_1.1.4       rlang_0.4.10     pillar_1.6.4    
[13] withr_2.4.0      glue_1.4.2       DBI_1.1.1        dbplyr_2.1.1     readxl_1.3.1     modelr_0.1.8    
[19] fortunes_1.5-4   lifecycle_1.0.1  cellranger_1.1.0 munsell_0.5.0    gtable_0.3.0     rvest_0.3.6     
[25] memoise_1.1.0    evaluate_0.14    knitr_1.30       ps_1.5.0         curl_4.3         fansi_0.4.2     
[31] urltools_1.7.3   triebeard_0.3.0  broom_0.7.3      Rcpp_1.0.7       scales_1.1.1     backports_1.2.1 
[37] jsonlite_1.7.2   fs_1.5.0         hms_1.0.0        pingr_2.0.1      digest_0.6.27    stringi_1.5.3   
[43] processx_3.4.5   cowplot_1.1.1    grid_4.0.3       rprojroot_2.0.2  cli_2.2.0        tools_4.0.3     
[49] magrittr_2.0.1   crayon_1.3.4     pkgconfig_2.0.3  ellipsis_0.3.2   xml2_1.3.2       reprex_0.3.0    
[55] datapasta_3.1.0  assertthat_0.2.1 rmarkdown_2.6    httr_1.4.2       rstudioapi_0.13  R6_2.5.0        
[61] speedtest_0.2.0  compiler_4.0.3

Has anyone come across this issue before? thanks

Upvotes: 1

Views: 220

Answers (1)

y3kMyRon
y3kMyRon

Reputation: 71

You could make use of rowwise() If you don't want to load data.table just for its between:

library(lubridate)
library(tidyverse)

df %>% 
rowwise() %>% 
mutate(y_2014 = if_else(between(2014, year(date1), year(date2)), 1, 0, as.numeric(NA)))

# A tibble: 3 x 3
# Rowwise: 
  date1      date2      y_2014
  <date>     <date>      <dbl>
1 2011-09-18 2012-02-18      0
2 2013-03-06 2014-03-06      1
3 2013-08-08 2015-02-03      1

Maybe use ungroup() if you want to make some other mutates or transmutes or summarizes on your data-table

Upvotes: 1

Related Questions