ABSTRACT

When is a crucial part of journalism’s classic Who, What, Where, When, and Why? But in data analysis, a user often wants to do more with dates than just report when something happened (or is expected to happen). This chapter presents some basics of using dates in R. It takes a look at New York City public restaurant-inspection data, calculating how long it takes for follow-up inspections after a restaurant is cited for a critical violation. The chapter presents working through a real-world dilemma where data needs to be reformatted. It covers turning a string like “6/27/2019” into an R date object, doing date calculations with both base R and the lubridate package, finding prior and next values with dplyr’s lead() and lag(), and dealing with times. Regular + and - operators work on date objects as well as numbers. mdy(“6/21/2019”) + 1 returns a date object one day later than 6/21/2019.