Some useful functions to wrangle with time series data

Creating data and time objects

The lubridate package in R provides a set of functions for working with dates and times.

year-month-date ymd function

ymd function converts a character vector of dates in the format “year-month-day” to a Date object. The name of the function stands for “year-month-day”, which is the order of the components in the date format that the function expects.

library(lubridate)
dates.ex1 <- c("2022-03-15", "2022-03-16", "2022-03-17")
class(dates.ex1)
[1] "character"
dates1 <- ymd(dates.ex1)
dates1
[1] "2022-03-15" "2022-03-16" "2022-03-17"
class(dates1)
[1] "Date"

year-month-day ymd function

The ymd() function also supports variations of the “year-month-day” format, such as “year/month/day” or “year.month.day”.

dates.ex2 <- c("2022/03/15", "2022/03/16", "2022/03/17")
dates2 <- ymd(dates.ex2)
dates2
[1] "2022-03-15" "2022-03-16" "2022-03-17"
class(dates2)
[1] "Date"
dates.ex3 <- c("2022/03/15", "2022/03/16", "2022/03/17")
dates2 <- ymd(dates.ex2)
dates2
[1] "2022-03-15" "2022-03-16" "2022-03-17"
class(dates2)
[1] "Date"

month-date-year mdy function

mdy(c("03-15-2023", "03-16-2023"))
[1] "2023-03-15" "2023-03-16"

date-month-year dmy function

dmy(c("15-03-2023", "16-03-2023"))
[1] "2023-03-15" "2023-03-16"

dates with strings

ydm("2023, March 15th")
Warning: All formats failed to parse. No formats found.
[1] NA
mdy("March 15th, 2023")
[1] "2023-03-15"
mdy("Mar 15th, 2023")
[1] "2023-03-15"
mdy("Wednesday, March 15th, 2023")
[1] "2023-03-15"
mdy("15th, March, 2023")
Warning: All formats failed to parse. No formats found.
[1] NA

hour-minutes-second hms working with dates and times

hms("10:30:00")
[1] "10H 30M 0S"

ymd_hm, ymd_hms and mdy_hms

ymd_hm("2023-03-15 10:30")
[1] "2023-03-15 10:30:00 UTC"
ymd_hms("2023-03-15 10:30:00")
[1] "2023-03-15 10:30:00 UTC"
mdy_hms("03-15-2023 10:30:00")
[1] "2023-03-15 10:30:00 UTC"

parse_date_time

If you have a date format that doesn’t match any of the built-in formats, you can use the parse_date_time() function in lubridate to specify a custom format.

dates.ex4 <- c("2022/03/15 10:30:00", "2022/03/16 11:00:00", "2022/03/17 12:15:00")

# Define the custom format using the format string
date_format <- "%Y/%m/%d %H:%M:%S"

# Convert the character vector to POSIXct objects using parse_date_time()
parse_date_time(dates.ex4, date_format)
[1] "2022-03-15 10:30:00 UTC" "2022-03-16 11:00:00 UTC"
[3] "2022-03-17 12:15:00 UTC"

Creating a sequence of time

y1 <- seq(as.Date("2012-01-01"),as.Date("2012-12-01"), by = "1 month")
y1
 [1] "2012-01-01" "2012-02-01" "2012-03-01" "2012-04-01" "2012-05-01"
 [6] "2012-06-01" "2012-07-01" "2012-08-01" "2012-09-01" "2012-10-01"
[11] "2012-11-01" "2012-12-01"
class(y1)
[1] "Date"
seq(as.Date("2023-03-01"),as.Date("2023-04-01"), by = "1 week")
[1] "2023-03-01" "2023-03-08" "2023-03-15" "2023-03-22" "2023-03-29"

Extract various time components

Let’s see how to extract various time components from the elements of the following Date object.

y2023 <- seq(as.Date("2023-01-01"),as.Date("2023-12-31"), by = "1 month")
y2023
 [1] "2023-01-01" "2023-02-01" "2023-03-01" "2023-04-01" "2023-05-01"
 [6] "2023-06-01" "2023-07-01" "2023-08-01" "2023-09-01" "2023-10-01"
[11] "2023-11-01" "2023-12-01"

year(): Extracts the year component

year(y2023)
 [1] 2023 2023 2023 2023 2023 2023 2023 2023 2023 2023 2023 2023

quarter(): Extracts the quarter

quarter(y2023)
 [1] 1 1 1 2 2 2 3 3 3 4 4 4

month(): Extracts the month component

month(y2023)
 [1]  1  2  3  4  5  6  7  8  9 10 11 12

week(): Extracts the week component

week(y2023)
 [1]  1  5  9 13 18 22 26 31 35 40 44 48

day(): Extracts the day component

day(y2023)
 [1] 1 1 1 1 1 1 1 1 1 1 1 1

To illustrate the remaining functions I use the following example

mydate <- ymd_hms("2023-03-15 10:30:01")

hour(): Extracts the hour component

hour(mydate)
[1] 10

minute(): Extracts the minute component

minute(mydate)
[1] 30

second(): Extracts the second component

second(mydate)
[1] 1

Related