Giter Club home page Giter Club logo

hot-dates's Introduction

Gaps-in-Dates

Gaps between dates

In my work, I frequently have to determine if a date was between two other dates. This could be fiscal years, academic terms, program dates, etc. This works really well if your start and end dates don't line up. For example, school terms may end in May then start again in August.

For this example, I'll use the flights dataset from library(nycflights13). For this example, I need 1 column of dates, so I cleaned up the above dataset like so:

flights %>% 
    select(flight, year, month, day) %>% 
    unite(date, year,month, day, sep="-")

I typically first, tell R that I am working with a date. There are many different formats and R is smart.

Here's a generic example if your column is already a character vector:

df %>%
  mutate(date = as.Date(date, "%y-%b-%d")) 

Let's say in this example, we are interested in flights around holidays, these are usually +/-3 days around holidays. Define date ranges and format as a date for those holiday ranges, let's just use Thanksgiving, Christmas, and New Year's.

I've built a dataframe of holidays from the Holidays R package. Then added 3 days from date for end date and subtracted 3 days from date for start.

image

Now using library(fuzzyjoin), you can tell R to join based on loose dates.

fuzzy_left_join(
  flights, holidays,
  by = c("date" = "start", #if it's after the start date
         "date" = "end"), #but before the end date
  match_fun = c(`>=`, `<=`) #as specified here
) 

Your output will have the holiday that the flight lines up with if the flight date was +/- 3 days from one of the perscribed holdiays. If the column holiday is NA, then your flight did not leave during a busy holiday time. fuzzy_joins() and joins are nice alternatives to clunky case_whens().

hot-dates's People

Contributors

lsouthard avatar

Watchers

 avatar

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.