Coder Social home page Coder Social logo

Assertion about dates about assertr HOT 3 CLOSED

TonyLadson avatar TonyLadson commented on August 17, 2024
Assertion about dates

from assertr.

Comments (3)

tonyfischetti avatar tonyfischetti commented on August 17, 2024

Thanks for the kind words....

There's no built-in approach yet. If I add this functionality to assertr (and the only reason I wouldn't is if it makes more sense to put it in another package) I would do it like this...

let's say I got a file like this called dates.csv

item,the_date
a,2016-09-02
b,2016-09-03
c,2016-09-03
d,2016-09-04
e,2016-09-04
f,2016-09-28
g,2016-09-29
h,2016-09-28

And I want to make sure all the dates are between September 1st and 30th...

library(lubridate)   # for the ymd (year-month-day), %within%, and %--% interval builder functions

these <- readr::read_csv("./dates.csv")

within_dates <- function(start_date, end_date){
  interval <- lubridate::`%--%`(start_date, end_date)
  closure <- function(adate){
    return(lubridate::`%within%`(adate, interval))
  }
  return(closure)
}

these %>%
  assert(within_dates(mdy("09-01-16"), mdy("09-30-16")), the_date)

The within_dates function is a lot like how within_bounds is implemented.

You can also achieve this with an anonymous function:

these %>%
    assert(function(x){x %within% (mdy("09-01-16") %--% mdy("09-30-16"))}, the_date)

Do these help?

from assertr.

TonyLadson avatar TonyLadson commented on August 17, 2024

Thank you, very helpful. I'm using these approaches already.

from assertr.

tonyfischetti avatar tonyfischetti commented on August 17, 2024

I'll put an example of this in the vignette!

from assertr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.