Date and Time values in R

This is an R Markdown document. Markdown is a simple formatting syntax for authoring web pages (click the Help toolbar button for more details on using R Markdown).

When you click the Knit HTML button a web page will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

current location

Sys.setlocale()
## [1] "pt_PT.UTF-8/pt_PT.UTF-8/pt_PT.UTF-8/C/pt_PT.UTF-8/pt_PT.UTF-8"

current date

today <- Sys.Date()
#class(today)
print(today)
## [1] "2017-03-30"

current time

now <- Sys.time()
#class(now)
print(now)
## [1] "2017-03-30 16:03:58 WEST"

#since January 1st 1970 at midnight

as.numeric(today)
## [1] 17255

#date is in ISO 8601 format already (i.e. YYYY-MM-DD HH:MM:SS). It just adds the current time zone of your computer by default

#create a date
a <- as.Date("2017-03-31") 
print(a)
## [1] "2017-03-31"
#create a date
b<-as.Date("2017/03/31")
print(b)
## [1] "2017-03-31"
# specify the format
c<-as.Date("3/31/2017",format='%m/%d/%Y')
print(c)
## [1] "2017-03-31"
# specify the format
d<-as.Date("Março 31, 2017",format='%B %d, %Y')
print(d)
## [1] "2017-03-31"
# specify the format
e<-as.Date('31MAR17',format='%d%b%y')   
print(e)
## [1] "2017-03-31"

Typically programming languages use a default origin of 1970-01-01

# alternate method with specified units
bdays = c(Denis=as.Date("1987-05-19"),Sofia=as.Date("7/10/1987", format='%m/%d/%Y'),
          Carmo=as.Date("19820912", format='%Y%m%d'), Luis=as.Date("27-07-88", format='%d-%m-%y'))

weekdays(bdays)
##          Denis          Sofia          Carmo           Luis 
##  "Terça Feira"  "Sexta Feira"      "Domingo" "Quarta Feira"
# take a difference - Time differences in days
difftime(Sys.Date(), bdays, units = "days")
## Time differences in days
## Denis Sofia Carmo  Luis 
## 10908 10856 12618 10473

suppose we have a time in seconds since 1500-04-22 00:00:00, EXCEL dates

z <- c(10485849600, 10477641600, 10561104000, 10562745600)
##ways to convert this
f<-as.Date(as.POSIXct(z, origin = "1500-04-22"))
print(f)
## [1] "1832-08-03" "1832-04-30" "1834-12-22" "1835-01-10"
g<- as.POSIXct("2017-03-30 14:00:00")
print(g)
## [1] "2017-03-30 14:00:00 WEST"
h <- as.POSIXct("2017-03-30 14_00_00", format='%Y-%m-%d %H:%M:%S')
print(h)
## [1] NA
i <- as.POSIXct("2017-03-30 14_00_00", format='%Y-%m-%d %H_%M_%S')
print(i)
## [1] "2017-03-30 14:00:00 WEST"

Exercise:

# use date and time, to calculate duration of day
dts <- data.frame(day = c("20081101", "20081102", "20081103", "20081104", "20081105", "20081106",
    "20081107"), time_start = c("06:30:00", "06:15:00", "07:12:20:00","07:30:00", "06:45:00", "06:25:00", "06:37:00"), time_end = c("23:30:00", "23:15:00", "22:12:20:00","21:30:00", "23:45:00", "22:25:00", "23:57:00"))
dts1 <- paste(dts$day, dts$time_start)
dts2 <- paste(dts$day, dts$time_end)
dts3 <- as.POSIXct(dts1, format = "%Y%m%d %H:%M:%S")
dts4 <- as.POSIXct(dts2, format = "%Y%m%d %H:%M:%S")

dts.all <- data.frame(dts, start = dts3, end=dts4)

str(dts.all)
## 'data.frame':    7 obs. of  5 variables:
##  $ day       : Factor w/ 7 levels "20081101","20081102",..: 1 2 3 4 5 6 7
##  $ time_start: Factor w/ 7 levels "06:15:00","06:25:00",..: 3 1 6 7 5 2 4
##  $ time_end  : Factor w/ 7 levels "21:30:00","22:12:20:00",..: 5 4 2 1 6 3 7
##  $ start     : POSIXct, format: "2008-11-01 06:30:00" "2008-11-02 06:15:00" ...
##  $ end       : POSIXct, format: "2008-11-01 23:30:00" "2008-11-02 23:15:00" ...
dts.all$difference<-difftime(dts.all$end, dts.all$start, units = c("secs"))

We can also plot day duration across the week, for example:

require(lubridate)
## Loading required package: lubridate
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
plot(round_date(dts.all$start, "day"), dts.all$difference, type = "o", col = "red",ylab="Duration    (in seconds)", xlab = "Weekday")
mtext(side = 4, line = 2.5, col = "red","Day duration")

plot of chunk unnamed-chunk-10