<- st_info %>%
st_info mutate(
Year = year(BD),
Month = month(BD),
Day = day(BD)
)
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.
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.
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)
)
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `Age = interval(Birthdate, Sys.Date())/years(1)`.
Caused by warning:
! All formats failed to parse. No formats found.
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"))
)
Working with Weekdays
You can get the weekday of a date.
<- st_info %>%
st_info mutate(
Weekday = wday(BD, label = TRUE)
)
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())
)
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")
)
Date Sequences
Creating sequences of dates, which might be useful for creating timelines or schedules.
<- seq.Date(from = min(st_info$BD),
date_sequence to = Sys.Date(),
by = "month")
Date Filtering
Filtering rows based on dates, like selecting entries from a specific year or month.
<- st_info %>%
st_info_2003 filter(Year == 2003)
Date Groups and Summarization
Grouping data by months or years and then summarizing other columns.
<- st_info %>%
st_info_summary 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.