Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

How to Work With Dates in R

R has a range of functions that allow you to work with dates and times. The easiest way of creating a date is to use the as.Date() function. For example, you write the opening day of the 2012 London Olympic Games as:

> xd <- as.Date("2012-07-27")
> xd
[1] "2012-07-27"
> str(xd)
 Date[1:1], format: "2012-07-27"

This works because the default format for dates in as.Date() is YYYY-MM-DD — four digits for year, and two digits for month and day, separated by a hyphen.

To find out what day of the week this is, use weekdays():

> weekdays(xd)
[1] "Friday"

You can add or subtract numbers from dates to create new dates. For example, to calculate the date that is seven days in the future, use the following:

> xd + 7
[1] "2012-08-03"

In the same way as with numbers or text, you can put multiple dates into a vector. To create a vector of seven days starting on July 27, add 0:6 to the starting date. (Remember: The colon operator generates integer sequences.)

> xd + 0:6
[1] "2012-07-27" "2012-07-28" "2012-07-29" "2012-07-30"
[5] "2012-07-31" "2012-08-01" "2012-08-02"

Because the weekdays() function takes vector input, it returns the days of the week for this sequence:

> weekdays(xd + 0:6)
[1] "Friday"  "Saturday" "Sunday"  "Monday"
[5] "Tuesday"  "Wednesday" "Thursday"

You can use the seq() function to create sequences of dates in a far more flexible way. As with numeric vectors, you have to specify at least three of the arguments (from, to, by, and length.out). However, in the case of Date objects, the by argument is very flexible. You specify by as a string consisting of a number followed by days, weeks, or months. Imagine you want to create a sequence of every second month of 2012, starting at January 1:

> startDate <- as.Date("2012-01-01")
> xm <- seq(startDate, by="2 months", length.out=6)
> xm
[1] "2012-01-01" "2012-03-01" "2012-05-01" "2012-07-01"
[5] "2012-09-01" "2012-11-01"

In addition to weekdays(), you also can get R to report on months() and quarters():

> months(xm)
[1] "January"  "March"   "May"    "July"
[5] "September" "November"
> quarters(xm)
[1] "Q1" "Q1" "Q2" "Q3" "Q3" "Q4"

The results of many date functions, including weekdays() and months() depends on the locale of the machine you’re working on. The locale describes elements of international customization on a specific installation of R. This includes date formats, language settings, and currency settings.

To find out some of the locale settings on your machine, use Sys.localeconv(). R sets the value of these variables at install time by interrogating the operating system for details. You can change these settings at runtime or during the session with Sys.setlocale().

To view the locale settings on your machine, try the following:

> Sys.localeconv()
Function Description
as.Date() Converts character string to Date
weekdays() Full weekday name in the current locale (for example, Sunday, Monday, Tuesday)
months() Full month name in the current locale (for example, January, February, March)
quarters() Quarter numbers (Q1, Q2, Q3, or Q4)
seq() Generates dates sequences if you pass it a Date object as its first argument
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.