# Ημερομηνίες και ημερολογιακά δεδομένα

### Athanassios Stavrakoudis
### astavrak@uoi.gr

library(tidyverse)
library(lubridate)
library(scales)
library(glue)

#this is not a date
d <- "1/10/2020"
is.Date(d)

# this is not a date
d <- "2020-10-01"
is.Date(d)

# this is a Date
d <- as.Date("2020-10-01")
d

# format a date
Sys.setlocale("LC_TIME", "en_US.UTF-8")
# format exports text/string

# year
format(d, "%Y")
format(d, "%y")

#month
format(d, "%m")
format(d, "%B")
format(d, "%b")

#day
format(d, "%d")
format(d, "%A")
format(d, "%a")

# day of
format(d, "%w")  # Weekday, Sunday -> 0
format(d, "%U")  # Sunday starting
format(d, "%W")  # Monday Starting
format(d, "%j")  # of the year

## full format
format(d, "%Y-%m-%d")
format(d, "%d/%m/%y")
format(d, "%m/%d/%Y")
format(d, "%A, %d %B %Y")
format(d, "%A, %d of %B %Y")

x <- format(d, "%A, %d %B %Y")
is.character(x)
as.Date(x)
as.Date(x, format = "%A, %d %B %Y")

# maybe conversion to numeric is needed sometimes
x <- format(d, "%j")
is.numeric(x)
is.character(x)
x <- as.numeric(x)
is.numeric(x)

# simple date arithmetics
d + 1
d + 150
d - 1
d - 150

d + weeks(2)
d - weeks(2)

d + months(3)
d - months(3)

d + years(1)
d - years(1)

# more complex arithmetics
d + months(1) + weeks(1)
d - months(3) + weeks(1)

## intervals between two dates
d1 <- Sys.Date()
d2 <- as.Date("2019-05-26")
d3 <- as.Date("2021-05-01")

difftime(d1, d2)
difftime(d1, d2, unit = "days")
difftime(d1, d2, unit = "weeks")

# lubridate intervals
di <- interval(d2, d1)
time_length(di, unit = "day")
time_length(di, unit = "week")
time_length(di, unit = "month")
time_length(di, unit = "year")

di <- interval(d1, d3)
time_length(di, unit = "day")
time_length(di, unit = "week")
time_length(di, unit = "month")
time_length(di, unit = "year")

## floor /ceiling of a date, returns Date
d1

floor_date(d1, unit = "week")
floor_date(d1, unit = "week", week_start = 7) # Sunday
floor_date(d1, unit = "week", week_start = 1) # Monday

floor_date(d1, unit = "month")
floor_date(d1, unit = "bimonth")
floor_date(d1, unit = "quarter")
floor_date(d1, unit = "halfyear")
floor_date(d1, unit = "year")

x <- floor_date(d1, unit = "year")
is.character(x)
is.Date(x)

# first date of next period
ceiling_date(d1, unit = "week")
ceiling_date(d1, unit = "week", week_start = 7) # Sunday
ceiling_date(d1, unit = "week", week_start = 1) # Monday

ceiling_date(d1, unit = "month")
ceiling_date(d1, unit = "bimonth")
ceiling_date(d1, unit = "quarter")
ceiling_date(d1, unit = "halfyear")
ceiling_date(d1, unit = "year")

# last date of current period
ceiling_date(d1, unit = "week") - 1
ceiling_date(d1, unit = "month") - 1
ceiling_date(d1, unit = "bimonth") - 1
ceiling_date(d1, unit = "quarter") - 1
ceiling_date(d1, unit = "halfyear") - 1
ceiling_date(d1, unit = "year") - 1

## construct Date object from numbers and strings

# numbers
y <- 2020
m <- 10
d <- 1

paste(y, m, d, sep = "-")
x <- paste(y, m, d, sep = "-")
as.Date(x)
ymd(x)

paste(d, m, y, sep = "-")
x <- paste(d, m, y, sep = "-")
as.Date(x) # fail
dmy(x) # pass

x <- glue("{y}-{m}-{d}")
as.Date(x)
ymd(x)

x <- glue("{d}/{m}/{y}")
as.Date(x) # fail
dmy(x) # pass

# parts as strings
d <- "01"
m <- "10"
y <- "2020"

paste(y, m, d, sep = "-")
x <- paste(y, m, d, sep = "-")
as.Date(x)
ymd(x)

x <- glue("{y}-{m}-{d}")
as.Date(x)
ymd(x)

# mixed parts
d <- 1
m <- "October"
y <- 2020

x <- paste(d, m, y, sep = "-")
x
as.Date(x)
as.Date(x, format = "%d-%B-%Y")
dmy(x)

# convert formatted strings to dates
d <- "01/10/20"
as.Date(d)
as.Date(d, format = "%d/%m/%y")
dmy(d)

d <- "01/10/2020"
as.Date(d)
as.Date(d, format = "%d/%m/%Y")
dmy(d)

d <- "10/01/2020"
as.Date(d)
as.Date(d, format = "%m/%d/%Y")
mdy(d)

## Date sequences

# between two dates
d1 <- as.Date("2020-10-01")
d2 <- as.Date("2021-12-31")

seq.Date(from = d1, to = d2, by = "15 day")
seq.Date(from = d1, to = d2, by = "1 week")
seq.Date(from = d1, to = d2, by = "2 month")
seq.Date(from = d1, to = d2, by = "1 quarter")
seq.Date(from = d1, to = d2, by = "1 year")

seq.Date(from = d1, by = "1 month", length.out = 15)
seq.Date(from = d1, by = "4 week", length.out = 4)

seq.Date(from = d1, to = d2, length.out = 12)

# first Monday of next month
d <- Sys.Date() + months(1)
x <- seq.Date(from = floor_date(d, unit = "month"),
to = ceiling_date(d, unit = "month") - 1,
by = "1 day")
x[wday(x, week_start = 1) == 1 & day(x) <= 7]

# last Friday of previous month
d <- Sys.Date() - months(1)
x <- seq.Date(from = floor_date(d, unit = "month"),
to = ceiling_date(d, unit = "month") - 1,
by = "1 day")
x[wday(x, week_start = 1) == 5 & day(x) > length(x) - 7]

f0 <- as.Date("2020-10-31")
f1 <- floor_date(f0, unit = "month") - 1
f0
f1



Συνδεθείτε για περισσότερες δυνατότητες αλληλεπίδρασης,
σχολιασμοί, εξωτερικοί σύνδεσμοι, βοήθεια, ψηφοφορίες, αρχεία, κτλ.

Εκπαιδευτικό υλικό από τον Αθανάσιο Σταυρακούδη σας παρέχετε κάτω από την άδεια Creative Commons Attribution-NonCommercial-ShareAlike 4.0 License.
Σας παρακαλώ να ενημερωθείτε για κάποιους επιπλέον περιορισμούς
http://stavrakoudis.econ.uoi.gr/stavrakoudis/?iid=401.