Date functions

Here are some essential date-based calculations and manipulations that one might need to perform using R and particularly with the tidyverse set of packages.

1 Year, Month, and Day Extraction

Extracting the year, month, and day from a date column. You can use the year(), month(), and day() functions from the lubridate package.

st_info <- st_info %>%
  mutate(
    Year = year(BD),
    Month = month(BD),
    Day = day(BD)
  )

2 Calculating Age

You can calculate the age by subtracting the birth date from the current date.

st_info <- st_info %>%
  mutate(
    Age = interval(Birthdate, Sys.Date()) / years(1)
  )

3 Calculating Difference Between Dates

The difference between dates can be calculated, which could be useful for calculating durations.

st_info <- st_info %>%
  mutate(
    Days_Since_Birth = as.numeric(difftime(Sys.Date(), 
                                           BD, 
                                           units = "days"))
  )

4 Working with Weekdays

You can get the weekday of a date.

st_info <- st_info %>%
  mutate(
    Weekday = wday(BD, label = TRUE)
  )

5 Creating Intervals

Creating intervals between two dates, useful to understand the duration between two dates.

st_info <- st_info %>%
  mutate(
    BD_to_Today = interval(BD, Sys.Date())
  )

6 Rounding Dates

Dates can be rounded to the nearest day, month, or year.

st_info <- st_info %>%
  mutate(
    Rounded_Date = round_date(BD, unit = "month")
  )

7 Date Sequences

Creating sequences of dates, which might be useful for creating timelines or schedules.

date_sequence <- seq.Date(from = min(st_info$BD), 
                          to = Sys.Date(), 
                          by = "month")

8 Date Filtering

Filtering rows based on dates, like selecting entries from a specific year or month.

st_info_2003 <- st_info %>%
  filter(Year == 2003)

9 Date Groups and Summarization

Grouping data by months or years and then summarizing other columns.

st_info_summary <- st_info %>%
  group_by(Year) %>%
  summarize(
    Count = n(),
    .groups = 'drop'
  )

All these date-based operations will enable more robust and flexible analyses, especially when dealing with time series data, birthdays, schedules, and any data that has a temporal component.