Showing posts with label R date time. Show all posts
Showing posts with label R date time. Show all posts

Saturday, March 17, 2012

R - dates with lubridate

datetime

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
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
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

Date: 2012-03-17 12:42:50 CET

Author: Mandy

Org version 7.6 with Emacs version 23

Validate XHTML 1.0

Saturday, August 13, 2011

R - extract year, month etc.... from date

extract year, month, day… from date

  • create vector of dates in standard format
my.dates <- as.Date(format(ISOdatetime(2000:2009,1:10,1:5,0,0,0),"%Y-%m-%d"))
my.dates
 [1] "2000-01-01" "2001-02-02" "2002-03-03" "2003-04-04" "2004-05-05"
 [6] "2005-06-01" "2006-07-02" "2007-08-03" "2008-09-04" "2009-10-05"
(make sure your object is of date fromat, check it with str(your.object)
  • extract years using format:
my.years <- format(my.dates,"%Y") # %y without century
my.years
 [1] "2000" "2001" "2002" "2003" "2004" "2005" "2006" "2007" "2008" "2009"
  • or months
my.months <- format(my.dates,"%m")
my.months
 [1] "01" "02" "03" "04" "05" "06" "07" "08" "09" "10"
  • or names of month (in current local)
my.months <- format(my.dates,"%b") # %B for long form
my.months
 [1] "Jan" "Feb" "Mär" "Apr" "Mai" "Jun" "Jul" "Aug" "Sep" "Okt"
  • or days (of month)
my.days <- format(my.dates,"%d")
my.days
 [1] "01" "02" "03" "04" "05" "01" "02" "03" "04" "05"
  • or week day (in current local)
my.days <- format(my.dates,"%a") # %A for long form, %w (0-6) of %u (1-7) for number
my.days
 [1] "Sa" "Fr" "So" "Fr" "Mi" "Mi" "So" "Fr" "Do" "Mo"
  • or day of year
my.days <- format(my.dates,"%j")
my.days
 [1] "001" "033" "062" "094" "126" "152" "183" "215" "248" "278"
  • or week of year (sunday as first day of the week)
my.weeks <- format(my.dates,"%U") # %W for monday = first day of the week
my.weeks
 [1] "00" "04" "09" "13" "18" "22" "27" "30" "35" "40"
  • local format of date
my.days <- format(my.dates,"%x")
my.days
 [1] "01.01.2000" "02.02.2001" "03.03.2002" "04.04.2003" "05.05.2004"
 [6] "01.06.2005" "02.07.2006" "03.08.2007" "04.09.2008" "05.10.2009"

Thursday, July 7, 2011

R - generate a vector with names of months (like LETTERS)

> format(ISOdatetime(2000,1:12,1,0,0,0),"%b")
gives:
 [1] "Jan" "Feb" "Mrz" "Apr" "Mai" "Jun" "Jul" "Aug" "Sep" "Okt" "Nov" "Dez"
where the month names depend on local settings. If you use %B instead of %b you get the non abbreviated months. How ever, there is also an easier way if you want the months in English: the vector
> month.name
 
gives
 [1] "January"   "February"  "March"     "April"     "May"       "June"     
 [7] "July"      "August"    "September" "October"   "November"  "December" 
or
> month.abb
 
gives
 [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"

Wednesday, June 8, 2011

R - converting integers or strings into date format

  • as.Date(x,format="")
  • dates are given in a lot of different formats, e.g.
    • "2011-03-14"
    • "2011-Mar-14"
    • "11-March-14"
  • if you want to convert them you have to tell R which format your dates have, here some examples:
  • > as.Date("2011-03-14",format="%Y-%m-%d")
    [1] "2011-03-14"
    > as.Date("2011/03/14", format="%Y/%m/%d")
    [1] "2011-03-14"
    > as.Date("14.3.2011", format="%d.%m.%Y")
    [1] "2011-03-14"
    > as.Date("2011-Sep-14", format="%Y-%b-%d")
    [1] "2011-09-14"
    > as.Date("2011-september-14", format="%Y-%B-%d")
    [1] "2011-09-14"
    > as.Date("14. November 1999", format="%d. %B %Y")
    [1] "1999-11-14"
  • here are the codes:
    Code Value
    %d Day of the month (decimal number)
    %m Month (decimal number)
    %b Month (abbreviated)
    %B Month (full name)
    %y Year (2 digits)
    %Y Year (4 digits)
  • if the date is given as integer you have to specify the origin - R counts in days. e.g. in SPSS  a date is stored in seconds, the origin is 1582-10-14. So you have divide by 86400 (=24*60*60); for example if 1000000 is given the command is:
  • > as.Date(1000000/86400, origin="1582-10-14")
    [1] "1582-10-25"

     the origin in Excel: 1900-01-01; SAS: 1960-01-01; R: 1970-01-01 

Friday, June 3, 2011

R - convert date into day of year

strptime contains a lot of functionality to converting a date (type ?strptime for details).

For converting a Y-m-d given date into a day-of-year number use the following syntax:

strptime(c("2011-03-04","2010-01-01"), "%Y-%m-%d")$yday+1

(you have to add 1 because the counting starts at 0)

if your date is given in a different format change the second argument within the brackets

e.g. for "01.10.1999":
strptime("01.10.1999, "%d.%m.%Y")$yday+1

more on dates with the lubridate package
more on dates with the format command