datetime
Table of Contents
1 package lubridate
1.1 reading dates with lubridate
- lubridate is a relatively new package which allows to handle date and date time formats in a more convenient way than it was possible with
format()
- first load the package
library(lubridate)
- if you want to read date or convert some data into date/date time format you can use
ymd()
- the first argument to
ymd()
have to be numeric oder string vector of suspected dates
- it should be used if there is a year, month and time component in a arbitry order, seperated by one of the following seperators: "-", "/", ".", and ""
- analogous
ymd
should appear in one of the following order:ymd
,ydm
,mdy
,myd
,dmy
,dym
- there is a optional argument
tz
to specify which time zone to parse the date with (string, known by the OS)
ydm(12051103) # -> "1205-03-11 UTC" ymd(12051103) # -> "1205-11-03 UTC" dym(12051103) # -> "511-03-12 UTC" dmy(12051103) # -> "1103-05-12 UTC" mdy(12051103) # -> "1103-12-05 UTC" myd(12051103) # -> "511-12-03 UTC"
[1] "1205-03-11 UTC" [1] "1205-11-03 UTC" [1] "511-03-12 UTC" [1] "1103-05-12 UTC" [1] "1103-12-05 UTC" [1] "511-12-03 UTC"
- it can also deal with the year consisting of two digits
dym(120503) # -> "2005-03-12 UTC" dym(127503) # -> "1975-03-12 UTC"
[1] "2005-03-12 UTC" [1] "1975-03-12 UTC"
- extract information:
- day
- day
my.date <- dym(120503) day(my.date)
[1] 12
- week day (number)
wday(my.date)
[1] 7
- week day (string)
wday(my.date,label=T)
[1] Sat Levels: Sun < Mon < Tues < Wed < Thurs < Fri < Sat
- year
year(my.date)
[1] 2005
- month (number)
month(my.date)
[1] 3
- month (name of month)
month(my.date,label=T)
[1] Mar 12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec
- week
week(my.date)
[1] 11
- day of year
yday(my.date)
[1] 71
1.2 working with dates
- get the origin of the current time scale
origin
[1] "1970-01-01 GMT"
- get the current date
x <- today() x
[1] "2012-03-17"
- week of current day
week(x)
- two weeks later
week(x) <- week(x) + 2 x
- is also a Saturday
wday(x,label=T)
[1] Sat Levels: Sun < Mon < Tues < Wed < Thurs < Fri < Sat
- rounding:
floor_date()
,ceiling_date()
- rounding down and up to the nearest integer day value
- rounding down and up to the nearest integer day value
floor_date(my.date,"day") # -> "2005-03-12 UTC" ceiling_date(my.date,"day") # -> "2005-03-13 UTC"
[1] "2005-03-12 UTC" [1] "2005-03-13 UTC"
- rounding down and up to the nearest integer month value
floor_date(my.date,"month") # -> "2005-03-01 UTC" ceiling_date(my.date,"month") # -> "2005-04-01 UTC"
[1] "2005-03-01 UTC" [1] "2005-04-01 UTC"
- rounding down and up to the nearest integer year value
floor_date(my.date,"year") # -> "2005-01-01 UTC" ceiling_date(my.date,"year") # -> "2006-01-01 UTC"
[1] "2005-01-01 UTC" [1] "2006-01-01 UTC"
- is a year a leap-year (argument has to be a date)
leap_year(ymd("20000101"))
- create a time interval given start and end point
date1 <- ymd("2000-01-01") date2 <- ymd("2000-10-01") my.int <- new_interval(date1,date2) my.int
[1] 2000-01-01 UTC--2000-10-01 UTC
- check whether or not a date falls within an interval
date3 <- ymd("2001-01-02") date3 %within% my.int
[1] FALSE
or
date4 <- ymd("2000-06-02") date4 %within% my.int
[1] TRUE
- check whether or not an interval falls within an interval
my.int2 <- new_interval(date1,date4) my.int2 %within% my.int
[1] TRUE
No comments :
Post a Comment