Introduction to Date and Time in R

I. Ozkan

Spring 2025

References

Before Start: Videos

Date and Times - 1: R. Peng

Representing Times - 2: R. Peng

Date in R

as.Date() 
Sys.Date()  
weekdays()
seq.Date()

# Current Date and Time
Sys.Date()
## [1] "2025-02-27"
today <- Sys.Date()

format(today, "%d %m %y") 
## [1] "27 02 25"
Code Value
%d Day of month as number
%m Month as number
%b Month abbreviated
%B Month full name
%y Year, two digits
%Y Year, four digits
format(today, "%d %b %y")  # with month abbreviated   
## [1] "27 Feb 25"
format(today, "%d %B %y")  # with month full name   
## [1] "27 February 25"
format(today, "%d %B %Y")  # with month full name   
## [1] "27 February 2025"

as.Date() Function

as.Date(c("04-28-2015", "11-12-2016"), format = "%m-%d-%Y")
## [1] "2015-04-28" "2016-11-12"
format(as.Date(c("04-28-2015", "11-12-2016"), format = "%m-%d-%Y"), "%d %B %Y")
## [1] "28 April 2015"    "12 November 2016"
as.Date(c("07/14/17"), format = "%m/%d/%y")
## [1] "2017-07-14"
x <- c("1jan1960", "2jan1960", "31mar1960", "30jul1960")
x
## [1] "1jan1960"  "2jan1960"  "31mar1960" "30jul1960"
z <- as.Date(x, "%d%b%Y")
z
## [1] "1960-01-01" "1960-01-02" "1960-03-31" "1960-07-30"
# number of days since origin time.. 
as.Date(32768, origin = "1900-01-01") # number of days since 1900
## [1] "1989-09-19"
# a complex one 
datestring <- c("January 10, 2012 10:40", "December 9, 2011 9:10")

# strptime() function
(datestring_d <- strptime(datestring, "%B %d, %Y %H:%M"))
## [1] "2012-01-10 10:40:00 EET" "2011-12-09 09:10:00 EET"
str(datestring)
##  chr [1:2] "January 10, 2012 10:40" "December 9, 2011 9:10"
str(datestring_d)
##  POSIXlt[1:2], format: "2012-01-10 10:40:00" "2011-12-09 09:10:00"

Basic Operations

x <- as.Date("May 05, 2023", format = "%B %d, %Y")
x
## [1] "2023-05-05"
y <- as.Date("March 5, 2023", format = "%B %d, %Y")
y
## [1] "2023-03-05"
# subtraction: number of days 
x - y
## Time difference of 61 days
# adding and subtracting number
x
## [1] "2023-05-05"
x + 1
## [1] "2023-05-06"
x - 1
## [1] "2023-05-04"
y
## [1] "2023-03-05"
y + 1 
## [1] "2023-03-06"
y - 1
## [1] "2023-03-04"
# comparisons 
x > y
## [1] TRUE
y > x
## [1] FALSE

weekdays() and seq.Date() Functions

Sys.Date()
## [1] "2025-02-27"
weekdays(Sys.Date())
## [1] "Thursday"
weekdays(Sys.Date() + 1:10)
##  [1] "Friday"    "Saturday"  "Sunday"    "Monday"    "Tuesday"   "Wednesday"
##  [7] "Thursday"  "Friday"    "Saturday"  "Sunday"
seq.Date(from=as.Date("2020/1/1"), to=as.Date("2023/1/1"), by="quarter")
##  [1] "2020-01-01" "2020-04-01" "2020-07-01" "2020-10-01" "2021-01-01"
##  [6] "2021-04-01" "2021-07-01" "2021-10-01" "2022-01-01" "2022-04-01"
## [11] "2022-07-01" "2022-10-01" "2023-01-01"
seq(from=as.Date("2020/1/1"), to=as.Date("2023/1/1"), by="quarter")
##  [1] "2020-01-01" "2020-04-01" "2020-07-01" "2020-10-01" "2021-01-01"
##  [6] "2021-04-01" "2021-07-01" "2021-10-01" "2022-01-01" "2022-04-01"
## [11] "2022-07-01" "2022-10-01" "2023-01-01"
# by= one the ("day", "week", "month", "quarter" or "year")

seq.Date(from=as.Date("2020/1/1"), length.out = 5, by="quarter")
## [1] "2020-01-01" "2020-04-01" "2020-07-01" "2020-10-01" "2021-01-01"
seq.Date(from=as.Date("2020/1/1"), length.out = 5, by="month")
## [1] "2020-01-01" "2020-02-01" "2020-03-01" "2020-04-01" "2020-05-01"
seq.Date(from=as.Date("2020/1/1"), length.out = 5, by="day")
## [1] "2020-01-01" "2020-01-02" "2020-01-03" "2020-01-04" "2020-01-05"
seq.Date(from=as.Date("2020/1/1"), length.out = 5, by="year")
## [1] "2020-01-01" "2021-01-01" "2022-01-01" "2023-01-01" "2024-01-01"
x
## [1] "2023-05-05"
seq(from = x, length = 6, by = "week")
## [1] "2023-05-05" "2023-05-12" "2023-05-19" "2023-05-26" "2023-06-02"
## [6] "2023-06-09"
seq(y, length = 6, by = "week")
## [1] "2023-03-05" "2023-03-12" "2023-03-19" "2023-03-26" "2023-04-02"
## [6] "2023-04-09"
weekdays(seq(from = x, length = 6, by = "week"))  
## [1] "Friday" "Friday" "Friday" "Friday" "Friday" "Friday"
weekdays(seq(from = x, length = 6, by = "day"))  
## [1] "Friday"    "Saturday"  "Sunday"    "Monday"    "Tuesday"   "Wednesday"
weekdays(seq(from = x, length = 6, by = "month"))  
## [1] "Friday"    "Monday"    "Wednesday" "Saturday"  "Tuesday"   "Thursday"
weekdays(seq(from = x, length = 6, by = "quarter"))  
## [1] "Friday"   "Saturday" "Sunday"   "Monday"   "Sunday"   "Monday"
weekdays(seq(from = x, length = 6, by = "year"))  
## [1] "Friday"    "Sunday"    "Monday"    "Tuesday"   "Wednesday" "Friday"
x
## [1] "2023-05-05"
as.numeric(x)
## [1] 19482
x - as.Date("1970-01-01")
## Time difference of 19482 days

ts Objects

ts(data = NA, start = 1, end = numeric(), frequency = 1,
   deltat = 1, ts.eps = getOption("ts.eps"), class = , names = ) 

as.ts()  
is.ts()

Examples

first_ts <- ts(1:10, frequency = 4, start = c(1959, 2))
first_ts
##      Qtr1 Qtr2 Qtr3 Qtr4
## 1959         1    2    3
## 1960    4    5    6    7
## 1961    8    9   10
print(first_ts)
##      Qtr1 Qtr2 Qtr3 Qtr4
## 1959         1    2    3
## 1960    4    5    6    7
## 1961    8    9   10
print(first_ts, calendar = F)
## Time Series:
## Start = c(1959, 2) 
## End = c(1961, 3) 
## Frequency = 4 
##  [1]  1  2  3  4  5  6  7  8  9 10
# column wise
matrix(data=1:12,nrow=3)
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
# row wise
matrix(data=1:12,nrow=3, byrow = TRUE)
##      [,1] [,2] [,3] [,4]
## [1,]    1    2    3    4
## [2,]    5    6    7    8
## [3,]    9   10   11   12
# without column names
ts(matrix(1:28, 7), start = c(1961, 1), frequency = 12)
##          Series 1 Series 2 Series 3 Series 4
## Jan 1961        1        8       15       22
## Feb 1961        2        9       16       23
## Mar 1961        3       10       17       24
## Apr 1961        4       11       18       25
## May 1961        5       12       19       26
## Jun 1961        6       13       20       27
## Jul 1961        7       14       21       28
# with column names
second_ts <- ts(matrix(1:28, 7), start = c(1961, 1), frequency = 12,
   names = c("x_1","x_2","x_3","x_4"))

second_ts
##          x_1 x_2 x_3 x_4
## Jan 1961   1   8  15  22
## Feb 1961   2   9  16  23
## Mar 1961   3  10  17  24
## Apr 1961   4  11  18  25
## May 1961   5  12  19  26
## Jun 1961   6  13  20  27
## Jul 1961   7  14  21  28
is.ts(second_ts)
## [1] TRUE
is.ts(first_ts)
## [1] TRUE

Useful Functions for ts Objects

tsp() 
frequency()  
start()
end()
time()
window()
plot.ts() # only example 

tsp(first_ts)
## [1] 1959.25 1961.50    4.00
# first: 1959.25, second: 1961.50 freq: 4.00

tsp(second_ts)
## [1] 1961.0 1961.5   12.0
frequency(first_ts)
## [1] 4
frequency(second_ts)
## [1] 12
time(second_ts)
##           Jan      Feb      Mar      Apr      May      Jun      Jul
## 1961 1961.000 1961.083 1961.167 1961.250 1961.333 1961.417 1961.500
time(first_ts)
##         Qtr1    Qtr2    Qtr3    Qtr4
## 1959         1959.25 1959.50 1959.75
## 1960 1960.00 1960.25 1960.50 1960.75
## 1961 1961.00 1961.25 1961.50
# windowing 
window(second_ts, start=c(1961,2)) 
##          x_1 x_2 x_3 x_4
## Feb 1961   2   9  16  23
## Mar 1961   3  10  17  24
## Apr 1961   4  11  18  25
## May 1961   5  12  19  26
## Jun 1961   6  13  20  27
## Jul 1961   7  14  21  28
window(second_ts, start=c(1961,2), end=c(1961,6)) 
##          x_1 x_2 x_3 x_4
## Feb 1961   2   9  16  23
## Mar 1961   3  10  17  24
## Apr 1961   4  11  18  25
## May 1961   5  12  19  26
## Jun 1961   6  13  20  27
# extend the time series
window(second_ts, start=c(1961,2), end=c(1962,5), extend=TRUE) 
##          x_1 x_2 x_3 x_4
## Feb 1961   2   9  16  23
## Mar 1961   3  10  17  24
## Apr 1961   4  11  18  25
## May 1961   5  12  19  26
## Jun 1961   6  13  20  27
## Jul 1961   7  14  21  28
## Aug 1961  NA  NA  NA  NA
## Sep 1961  NA  NA  NA  NA
## Oct 1961  NA  NA  NA  NA
## Nov 1961  NA  NA  NA  NA
## Dec 1961  NA  NA  NA  NA
## Jan 1962  NA  NA  NA  NA
## Feb 1962  NA  NA  NA  NA
## Mar 1962  NA  NA  NA  NA
## Apr 1962  NA  NA  NA  NA
## May 1962  NA  NA  NA  NA
window(first_ts, start=c(1960,1), end=c(1961,1))
##      Qtr1 Qtr2 Qtr3 Qtr4
## 1960    4    5    6    7
## 1961    8
# modify the values 
window(first_ts, start=c(1960,1), end=c(1960,3)) <- NA

first_ts
##      Qtr1 Qtr2 Qtr3 Qtr4
## 1959         1    2    3
## 1960   NA   NA   NA    7
## 1961    8    9   10
plot(first_ts)

plot(second_ts)

plot(second_ts, plot.type="single", lty=1:4, xlab="X", ylab="Y",
     main="My First Multi-variate Time Series Plot")

Let’s Navigate Wikipedia for ISO 8601 Standard

ISO 8601: Wikipedia

Time in R

POSIXct Class

Sys.timezone()
## [1] "Europe/Istanbul"
x <- Sys.Date()
str(x)
##  Date[1:1], format: "2025-02-27"
x_1 <- as.POSIXct(x)
x_1
## [1] "2025-02-27 UTC"
x <- Sys.time()
str(x)
##  POSIXct[1:1], format: "2025-02-27 10:32:22"
x_p <- as.POSIXlt(x)
str(x_p)
##  POSIXlt[1:1], format: "2025-02-27 10:32:22"
names(unclass(x_p))
##  [1] "sec"    "min"    "hour"   "mday"   "mon"    "year"   "wday"   "yday"  
##  [9] "isdst"  "zone"   "gmtoff"
x_p$sec
## [1] 22.12668
x_p$hour
## [1] 10

Widely Used Packages for Time Series Objects