Last active
September 24, 2017 08:09
-
-
Save aravindhebbali/2b580575c59162c78c1ef30c1de55971 to your computer and use it in GitHub Desktop.
Working with Date & Time in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# install | |
install.packages('lubridate') | |
install.packages('readr') | |
install.packages('dplyr') | |
install.packages('magrittr') | |
# library | |
library('lubridate') | |
library('readr') | |
library('dplyr') | |
library('magrittr') | |
# import data | |
transact <- readr::read_csv('https://raw.githubusercontent.com/rsquaredacademy/datasets/master/transact.csv') | |
transact | |
# origin, date, time and timezone | |
lubridate::origin | |
now() | |
today() | |
am(now()) | |
pm(now()) | |
# Extract Date, Month & Year from Due Date | |
transact %>% | |
mutate( | |
due_day = day(Due), | |
due_month = month(Due), | |
due_year = year(Due) | |
) | |
# compute days to settle invoice | |
transact %>% | |
mutate( | |
days_to_pay = Payment - Invoice | |
) | |
# compute days over due | |
transact %>% | |
mutate( | |
delay = Payment - Due | |
) | |
# Is due year a leap year? | |
transact %>% | |
mutate( | |
due_year = year(Due), | |
is_leap = leap_year(due_year) | |
) | |
# If due day is February 29, is it a leap year? | |
transact %>% | |
mutate( | |
due_day = day(Due), | |
due_month = month(Due), | |
days_due_month = days_in_month(due_month), | |
due_year = year(Due), | |
is_leap = leap_year(due_year) | |
) %>% | |
select(-(Invoice), -(Payment)) %>% | |
filter(due_month == 2) | |
# how many invoices were settled within due date | |
transact %>% | |
mutate( | |
inv_due_interval = interval(Invoice, Due), | |
due_next = Due + days(1), | |
due_pay_interval = interval(due_next, Payment), | |
overlaps = int_overlaps(inv_due_interval, due_pay_interval) | |
) %>% | |
select(inv_due_interval, due_next, due_pay_interval, overlaps) | |
# using int_shift | |
transact %>% | |
mutate( | |
inv_due_interval = interval(Invoice, Due), | |
due_pay_interval = interval(Due, Payment), | |
due_pay_next = int_shift(due_pay_interval, by = days(1)), | |
overlaps = int_overlaps(inv_due_interval, due_pay_next) | |
) %>% | |
select(inv_due_interval, due_pay_next, overlaps) | |
transact %>% | |
mutate( | |
inv_due_interval = interval(Invoice, Due), | |
overlaps = Payment %within% inv_due_interval | |
) %>% | |
select(Due, Payment, overlaps) | |
# quarter | |
transact %>% | |
mutate( | |
Quarter = quarter(Due) | |
) | |
transact %>% | |
mutate( | |
Quarter = quarter(Due, with_year = TRUE) | |
) | |
# duration | |
transact %<>% | |
mutate( | |
days_to_pay = Payment - Invoice | |
) | |
transact %>% | |
mutate( | |
dseconds = duration(as.numeric(days_to_pay), 'days') | |
) | |
# interval | |
transact %<>% | |
mutate( | |
inv_pay_int = interval(Invoice, Payment) | |
) | |
# convert units | |
transact %>% | |
mutate( | |
inv_pay_days = inv_pay_int / ddays() | |
) | |
transact %>% | |
mutate( | |
inv_pay_days = time_length(inv_pay_int, unit = "days") | |
) | |
transact %>% | |
mutate( | |
inv_pay_days = as.period(inv_pay_int) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment