In this quick post we will see 14 simple tips that will help get started dealing with date as variable instead of a string. We will see tips on how to create a date variable in few different ways and get components of date variable using the R package lubdridate.
The R package lubdridate was originally developed by Garrett Grolemund, Hadley Wickham and published in 2011, Dates and Times Made Easy with lubridate. Till recently it has been a stand alone package and not a part of tidyverse meta package. At the 2022 RStudio Conference it was announced that lubridate is officially a part of tidyverse packages. This is big for a number of reasons. One is you can load it one swoop with library(tidyverse).
It's been a very long time coming, but I just added lubridate to the core tidyverse. Would love it if folks would remotes::install_github("tidyverse/tidyverse") and let me know if it causes any problems #rstats
— Hadley Wickham (@hadleywickham) August 12, 2022
Here are 14 simple tips to get started working with dates using some of the lubridate‘s functions.
1. How to get today’s date
We can use today() function to get today’s date.
today() [1] "2022-08-22"
2. How to create a date variable from string in year-month-day format
Using ymd() function in lubridate we can create date object using string year-month-day format.
# create date object from string date_from_ymd <- ymd("2022-08-22") date_from_ymd ## [1] "2022-08-22"
3. How to create date variable from string in month-day-year format
We can also create a date object using a string in month-day-year format using mdy() function.
# create date object from string date_from_mdy <- mdy("August 22nd 2022") date_from_mdy ## [1] "2022-08-22"
4. creating date variable from string in day-month-year format
Similarly we use dmy() function to create a date variable using a string in day-month-year format.
# create date object from string date_from_dmy <- dmy("21-Aug-2022") date_from_dmy ## [1] "2022-08-21"
5. Create date from year, month, and day separately
Sometimes you might have all the necessary date information separately, like year, month and day stored as variables in a dataframe. We can create date object from the date components using make_day() function.
First, let us create dataframe with the information needed for creating date variable.
Create date from year, month, and day separately df <- tibble(year=c(2022,2021), month=c(8,12), day=c(21, 1))
Our dataframe has year, month, and day as numerical variables.
df ## # A tibble: 2 × 3 ## year month day ## <dbl> <dbl> <dbl> ## 1 2022 8 21 ## 2 2021 12 1
With make_date() function we can combine the year, month, and day variables into a single date variable.
df %>% mutate(date=make_date(year,month,day)) ## # A tibble: 2 × 4 ## year month day date ## <dbl> <dbl> <dbl> <date> ## 1 2022 8 21 2022-08-21 ## 2 2021 12 1 2021-12-01
6. Get year from date
Sometimes we need exactly the opposite of the previous example, that we might need to get the components of date variables. like, year, month and day.
Using year() function we can get the year from a data variable
# data variable date1 ## [1] "2022-08-21" # get the year from date year(date1) ## [1] 2022
7. Get month from date variable
We can get month from a date variable using month() function on the date variable.
# get month from date variable month(date1) ## [1] 8
8. Get abbreviated month name from date variable
Note by default month() returns the month from a date variable as number. To get the name of the month in abbreviated form, we use labe=TRUE argument to month() function.
# get month name (abbrveiated) month(date1, label=TRUE) ## [1] Aug ## 12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec
9. Get full month name from date variable
To get the full name of the month in the date variable, we use both the arguments label=TRUE and abbr=FALSE.
# get full month name month(date1, label=TRUE, abbr=FALSE) ## [1] August ## 12 Levels: January < February < March < April < May < June < ... < December
10. Get day of the month from date variable
With mday() function, we can get the day of the month from a date object.
# day of the month mday(date1) ## [1] 21
11. Day of the week
Similarly we can get day of the week using wday() function.
# day of the week wday(date1) ## [1] 1
12. Day Name of the week
To get the name of the day abbreviated, we use label=TRUE as argument to wday() function.
wday(date1, label=TRUE) ## [1] Sun ## Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat
13. Full name of the Day of the week
Similarly to get the full name of the day we use label=TRUE and abbr=FALSE as arguments to wday() function.
date1 <- ymd("2022-08-21") # day the week day(date1, label=TRUE, abbr=FALSE) ## [1] Sunday ## 7 Levels: Sunday < Monday < Tuesday < Wednesday < Thursday < ... < Saturday
14. Get day of the year
If you are interested in the day of the year, use yday() function on the date variable.
yday(date1) ## [1] 233
15. How old is Hadley Wickham
A bonus tip is to find how old is Hadley Wickham (Thanks to R for Data Science Book as. an example to work with durations using lubridate package).
# How old is Hadley? hadley_age <- today() - ymd(19791014) hadley_age ## Time difference of 15653 days
as.duration(hadley_age) ## [1] "1352419200s (~42.86 years)"
1 comment
Comments are closed.