Skip to content

fcase doesn't recognize S4 object : lubridate example #4131

Description

@torema-ed

Thank you all for the hard work in creating fcase. I have tried it out on a project that I'm currently working on and encountered problems when it was used together with lubridate period creation functions. It seems to recognize lubridate::weeks, lubridate::months, lubridate::years as lubridate::days.

reprex:

# Loading packages --------------------------------------------------------

library(data.table)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:data.table':
#> 
#>     hour, isoweek, mday, minute, month, quarter, second, wday, week,
#>     yday, year
#> The following object is masked from 'package:base':
#> 
#>     date
library(lest)

# Creating test data ------------------------------------------------------

dt <- data.table(period_unit = c("days", "months", "weeks", "years"),
                 period_length = rep(1, times = 4))

# fcase output ------------------------------------------------------------

dt[, period := fcase(period_unit == "days", days(period_length),
                     period_unit == "months", months(period_length),
                     period_unit == "weeks", weeks(period_length),
                     period_unit == "years", years(period_length))][]
#>    period_unit period_length      period
#> 1:        days             1 1d 0H 0M 0S
#> 2:      months             1 1d 0H 0M 0S
#> 3:       weeks             1 1d 0H 0M 0S
#> 4:       years             1 1d 0H 0M 0S

# case_when output --------------------------------------------------------

dt[, period := case_when(period_unit == "days" ~ days(period_length),
                         period_unit == "months" ~ months(period_length),
                         period_unit == "weeks" ~ weeks(period_length),
                         period_unit == "years" ~ years(period_length))][]
#>    period_unit period_length            period
#> 1:        days             1       1d 0H 0M 0S
#> 2:      months             1    1m 0d 0H 0M 0S
#> 3:       weeks             1       7d 0H 0M 0S
#> 4:       years             1 1y 0m 0d 0H 0M 0S

Created on 2019-12-19 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.2 (2019-12-12)
#>  os       Linux Mint 18.3             
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language en_SG:en                    
#>  collate  en_SG.UTF-8                 
#>  ctype    en_SG.UTF-8                 
#>  tz       Asia/Singapore              
#>  date     2019-12-19                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date       lib source        
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 3.6.1)
#>  backports     1.1.5   2019-10-02 [1] CRAN (R 3.6.1)
#>  callr         3.4.0   2019-12-09 [1] CRAN (R 3.6.1)
#>  cli           2.0.0   2019-12-09 [1] CRAN (R 3.6.1)
#>  crayon        1.3.4   2017-09-16 [1] CRAN (R 3.6.1)
#>  data.table  * 1.12.9  2019-12-19 [1] local         
#>  desc          1.2.0   2018-05-01 [1] CRAN (R 3.6.1)
#>  devtools      2.2.1   2019-09-24 [1] CRAN (R 3.6.1)
#>  digest        0.6.23  2019-11-23 [1] CRAN (R 3.6.1)
#>  ellipsis      0.3.0   2019-09-20 [1] CRAN (R 3.6.1)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 3.6.1)
#>  fansi         0.4.0   2018-10-05 [1] CRAN (R 3.6.1)
#>  fs            1.3.1   2019-05-06 [1] CRAN (R 3.6.1)
#>  glue          1.3.1   2019-03-12 [1] CRAN (R 3.6.1)
#>  highr         0.8     2019-03-20 [1] CRAN (R 3.6.1)
#>  htmltools     0.4.0   2019-10-04 [1] CRAN (R 3.6.1)
#>  knitr         1.26    2019-11-12 [1] CRAN (R 3.6.1)
#>  lest        * 1.1.0   2019-11-29 [1] CRAN (R 3.6.1)
#>  lubridate   * 1.7.4   2018-04-11 [1] CRAN (R 3.6.1)
#>  magrittr      1.5     2014-11-22 [1] CRAN (R 3.6.1)
#>  memoise       1.1.0   2017-04-21 [1] CRAN (R 3.6.1)
#>  pkgbuild      1.0.6   2019-10-09 [1] CRAN (R 3.6.1)
#>  pkgload       1.0.2   2018-10-29 [1] CRAN (R 3.6.1)
#>  prettyunits   1.0.2   2015-07-13 [1] CRAN (R 3.6.1)
#>  processx      3.4.1   2019-07-18 [1] CRAN (R 3.6.1)
#>  ps            1.3.0   2018-12-21 [1] CRAN (R 3.6.1)
#>  R6            2.4.1   2019-11-12 [1] CRAN (R 3.6.1)
#>  Rcpp          1.0.3   2019-11-08 [3] CRAN (R 3.6.1)
#>  remotes       2.1.0   2019-06-24 [1] CRAN (R 3.6.1)
#>  rlang         0.4.2   2019-11-23 [1] CRAN (R 3.6.1)
#>  rmarkdown     2.0     2019-12-12 [1] CRAN (R 3.6.1)
#>  rprojroot     1.3-2   2018-01-03 [1] CRAN (R 3.6.1)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 3.6.1)
#>  stringi       1.4.3   2019-03-12 [1] CRAN (R 3.6.1)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 3.6.1)
#>  testthat      2.3.1   2019-12-01 [1] CRAN (R 3.6.1)
#>  usethis       1.5.1   2019-07-04 [1] CRAN (R 3.6.1)
#>  withr         2.1.2   2018-03-15 [1] CRAN (R 3.6.1)
#>  xfun          0.11    2019-11-12 [1] CRAN (R 3.6.1)
#>  yaml          2.2.0   2018-07-25 [1] CRAN (R 3.6.1)
#> 
#> [1] /home/edt/R/3.6/library
#> [2] /usr/local/lib/R/site-library
#> [3] /usr/lib/R/site-library
#> [4] /usr/lib/R/library

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions