Togaware DATA MINING
Desktop Survival Guide
by Graham Williams
Google


Time and Dates

Dates are read as factors into R from a CSV file. These can be easily converted to date objects using as.Date:

> ds <- read.csv("authors.csv")
> ds$Notified
   [1]            2005/06/05 2005/06/05 
> as.Date(ds$Notified, format="%Y/%m/%d")
   [1] NA           "2005-06-05" "2005-06-05"

The default format is "%Y-%m-%d". See the help for strftime for an explanation of the format. Any extra text found in the string after the text has been consumed by the format string will simply be ignored. But if the format is not found at the beginning of the string then a NA is returned.

> as.Date(c("2005-05-22 12:35:00", "2005-05-23 abc","abc 2005-05-24"))
[1] "2005-05-22" "2005-05-23" NA

To compare date values simply use as.Date:

if (ds$dt > as.Date("2005-12-15")) ...



> as.POSIXlt('2005-7-1')
[1] "2005-07-01"
> unlist(as.POSIXlt('2005-7-1'))
  sec   min  hour  mday   mon  year  wday  yday isdst 
    0     0     0     1     6   105     5   181     0



Copyright © 2004-2006 Graham.Williams@togaware.com
Support further development through the purchase of the PDF version of the book.
Brought to you by Togaware.