R Dates - gizotso/R GitHub Wiki

==========================================

Dates class

==========================================

Sys.Date() ##[1] "2015-02-05" Sys.time() ## [1] "2015-02-05 17:18:37 CET"
class(Sys.time())##[1] "POSIXct" "POSIXt" #POSIXct is a widely used framework for representing dates and times. In the POSIXct #framework, each time is represented by the number of seconds that have passed between #the time and 12:00 AM January 1st 1970 (in the Universal Time Coordinated (UTC) zone). unclass(now) ##[1] 1423760870 format(Sys.time(),"%H %M") ##[1] "17 19" date() ##[1] "Thu Feb 05 17:20:13 2015" format(Sys.time(), "%a %b %d %H:%M:%S %Y") ## [1] "jeu. févr. 05 17:25:23 2015"

something similar to date() in the current locale

mil = 1000000 class(mil) <- c("POSIXct", "POSIXt") mil ## [1] "1970-01-12 14:46:40 CET"

months in English

month.name

months in your current locale

format(ISOdate(2000, 1:12, 1), "%B") format(ISOdate(2000, 1:12, 1), "%b")

----------------

conversions

- see also lubridate package

----------------

my.date <- as.Date("2010-12-30") # default yyyy-mm-dd my.date2 <- as.Date("12/20/30", format="%m/%d/%y") print(my.date2) [1] "2030-12-20"

my.time <- strptime("12/20/30 14.34.35", format="%m/%d/%y %H.%M.%S") # input time and date print(my.time) ## [1] "2030-12-20 14:34:35 CET"

my.string <- as.character(Sys.time()) # convert a date/time object to a normal string

----------------

Extract information

----------------

weekdays() and months() returns results in the local language

weekdays(my.date) ##[1] "jeudi" months(my.date) julian(my.date)

old = Sys.getlocale() Sys.getlocale("LC_TIME") ##[1] "French_France.1252" Sys.setlocale("LC_TIME", "...") Sys.setlocale(locale = old)

sequences of dates

#create the 10 first days of January 2012 seq(from = as.Date("01/01/12", "%d/%m/%y"), to = as.Date("10/01/12","%d/%m/%y"), by = "day")

[1] "2012-01-01" "2012-01-02" "2012-01-03" "2012-01-04" "2012-01-05" "2012-01-06" "2012-01-07"

[8] "2012-01-08" "2012-01-09" "2012-01-10"

#create the 20th of each month in 2012 seq(from = as.Date("20/01/12", "%d/%m/%y"), to = as.Date("20/12/12","%d/%m/%y"), by = "month")

[1] "2012-01-20" "2012-02-20" "2012-03-20" "2012-04-20" "2012-05-20" "2012-06-20" "2012-07-20"

[8] "2012-08-20" "2012-09-20" "2012-10-20" "2012-11-20" "2012-12-20"

#create a sequence of every other day in january 2012 seq(from = as.Date("01/01/12", "%d/%m/%y"), to = as.Date("31/01/12","%d/%m/%y"), length.out = 16)

[1] "2012-01-01" "2012-01-03" "2012-01-05" "2012-01-07" "2012-01-09" "2012-01-11" "2012-01-13"

[8] "2012-01-15" "2012-01-17" "2012-01-19" "2012-01-21" "2012-01-23" "2012-01-25" "2012-01-27"

[15] "2012-01-29" "2012-01-31"

==========================================

Time series

class(ts) > [1] "ts"

==========================================

ts(1:10, start = 1959)

Time Series:

Start = 1959

End = 1968

Frequency = 1

[1] 1 2 3 4 5 6 7 8 9 10

ts(1:20, frequency = 12, start = c(1959, 2))

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

1959 1 2 3 4 5 6 7 8 9 10 11

1960 12 13 14 15 16 17 18 19 20

ts(1:7, frequency = 4, start = c(2014, 1))

Qtr1 Qtr2 Qtr3 Qtr4

2014 1 2 3 4

2015 5 6 7