January 1, 2022

date time operations in R

R Datetime operations & chron/lubridate packages


Sys.Date()
Sys.Date() + 12
typeof(Sys.Date())
as.integer(Sys.Date())
unclass(Sys.Date())

as.Date(x, format, …)
as.Date(z, tz = "NZ")
date1 <- as.Date('2017-09-28')
as.Date(32768, origin = "1950-01-01")
dates <- seq(as.Date("2017-01-01"), length = 5, by = "days")
six.weeks <- seq(dt1, length = 6, by = "2 weeks")
seq(as.Date("2013/1/1"), as.Date("2016/1/1"), by = "quarter")
%d - day
%m - month number
%b - month abbreviated
%B - month full name
%y - year in 2 digit
%Y - year in 4 digit
%H - hours in 24 hour format
%I - hours in 12 hour format
%M - minutes
%S - seconds
%P - PM/AM
as.Date("Aug 30,1970", format = "%b %d, %Y")
as.Date(dates, "%m/%d/%y")
z <- as.Date(x, "%d%b%Y")

Sys.time()
typeof(Sys.time())
unclass(Sys.time())
system.time(for(i in 1:100) mad(runif(1000)))
system.time(expr, gcFirst = TRUE)
unix.time(expr, gcFirst = TRUE)
Sys.timezone(location = TRUE)
Sys.timezone()
Sys.timezone(FALSE)
OlsonNames()
str(OlsonNames())
x <- .leap.seconds[1:3]

date2 <- as.POSIXct('2017-07-12 18:00') # POSIX calendar time
index(x) <- as.POSIXct(format(time(x)),tz="")
dt <- as.POSIXct("2012-12-10 15:18:01 EST")
as.POSIXct(sample((60*60*24*365*50):(60*60*24*365*55),20), origin = as.Date("1980-01-01"))
my.posixct2 <- my.posixct + 9010
as.POSIXct("2014-08-05 11:58:54 PDT") < as.POSIXct("2014-08-05 11:59:20 PDT")
as.POSIXlt(x, tz = "", …) # POSIX list
as.POSIXlt(Sys.time(), "America/New_York")
date_time = as.POSIXlt("2017-08-17 10:15:09", format = "%Y-%m-%d %H:%M:%S")
grades$sub_time<- as.POSIXlt(grades$sub_time, format = "%m/%d/%y-%H:%M:%S")

format(date_time, "%H")
format(xct, "%S minutes past %I %p, on %d %B %Y")
z <- ISOdate(2016, 05, 19, c(0,12))
weekdays(dates)
months(my_date)
table(months(df$date))
quarters(dates)
gold_silver$years <- format(gold_silver$Price_Date, "%Y")
dt2 - 10
xct + 4*60*60

difftime(time1, time2, tz, units = c("auto", "secs", "mins", "hours", "days", "weeks"))
difftime(tomorrow, today)
difftime(dt1, dt2, units = "weeks")
difftime(tomorrow, today, units = "secs")

strftime(x, format = "", tz = "", usetz = FALSE, …)
strptime(x, format, tz = "")
s = strptime("2017-02-23 16:18:17", format = "%Y-%m-%d %H:%M:%S")
temp.2 <- as.POSIXct(strptime(temp, '%d/%m/%Y'))

Sys.getlocale(category = "LC_ALL")
Sys.getlocale()
oloc <- Sys.getlocale("LC_CTYPE")
Sys.getlocale("LC_TIME")
Sys.setlocale(category = "LC_ALL", locale = "")
Sys.setlocale("LC_CTYPE", oloc)
Sys.setlocale("LC_MONETARY", locale = "")
Sys.setlocale(locale = old)
Sys.localeconv()
proc.time()
as.numeric(date1)
as.numeric(Sys.time())

julian(x, origin = as.POSIXct("1980-01-01", tz = "GMT"), ...)
julian(date_var)
julian(Sys.Date(), -2420568)
floor(as.numeric(julian(Sys.time())) + 2140687.5)
leap.year(y)

library(chron)
month.day.year(jul, origin.)
month.day.year(my.days)
month.day.year(my.days,origin=c(1,1,1984))
day.of.week(month, day, year)
tm1.c <- as.chron("2017-04-24 23:35:16")
tm2.c <- as.chron("09/22/17 08:12:27", "%m/%d/%y %H:%M:%S")
as.chron("2016-03-10 08:12:07") - as.chron("2016-03-09 23:59:46")

library(lubridate)
now()
today()
sales6[, date_cols] <- lapply(sales6[,date_cols], ymd)
dt <- parse_date_time(dt, c("dmy_HMS", "dmy_HM"))
tm1.lubri <- ymd_hms("2015-07-24 23:55:26")
tm2.lubri <- mdy_hm("07/25/14 08:32")
tm3.lubri <- ydm_hm("2017-25-07 4:00am")
tm4.lubri <- dmy("26072016")
year(tm1.lubri)
week(tm1.lubri)
hour(tm1.lubri)
tz(tm1.lubri)
wday(tm1.lubri, label = TRUE)
second(tm2.lubri) <- 8
round_date(tm1.lubri, "day")

Related aRticles: R string operations

No comments:

Post a Comment