Skip to main content

Dates

  • Chapter
  • 13k Accesses

Part of the book series: Use R! ((USE R))

R provides several options for dealing with date and date/time data. The builtin as.Date function handles dates (without times); the contributed package chron handles dates and times, but does not control for time zones; and the POSIXct and POSIXlt classes allow for dates and times with control for time zones. The general rule for date/time data in R is to use the simplest technique possible. Thus, for date only data, as.Date will usually be the best choice. If you need to handle dates and times, without time-zone information, the chron package is a good choice; the POSIX classes are especially useful when time-zone manipulation is important. Also, don't overlook the various “as.” functions (like as.Date and as.POSIXlt) for converting among the different date types when necessary.

Except for the POSIXlt class, dates are stored internally as the number of days or seconds from some reference date. Thus, dates in R will generally have a numeric mode, and the class function can be used to find the way they are actually being stored. The POSIXlt class stores date/time values as a list of components (hour, min, sec, mon, etc.) making it easy to extract these parts.

To get the current date, the Sys.Date function will return a Date object which can be converted to a different class if necessary.

This is a preview of subscription content, log in via an institution.

Buying options

Chapter
USD   29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD   69.99
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD   89.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Learn about institutional subscriptions

Preview

Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.

Rights and permissions

Reprints and permissions

Copyright information

© 2008 Springer Science+Business Media, LLC

About this chapter

Cite this chapter

(2008). Dates. In: Data Manipulation with R. Use R!. Springer, New York, NY. https://doi.org/10.1007/978-0-387-74731-6_4

Download citation

Publish with us

Policies and ethics