Togaware DATA MINING
Desktop Survival Guide
by Graham Williams
Google

Dates and Times

R has full support for dealing with dates and times. The date() function returns the current date as a string: Wed Oct 20 06:48:06 2004. The following example illustrates some of the basic functionality for date handling.

[width=0.5]rplot-date


pdf("graphics/rplot-date.pdf")
Time <- c("2004-08-05 09:08:48", "2004-08-13 20:53:38",
          "2004-08-14 13:57:23", "2004-08-12 16:17:41",
          "2004-08-12 16:15:27", "2004-08-11 21:38:24",
          "2004-08-12 14:28:41", "2004-08-18 18:04:47",
          "2004-08-13 15:23:14", "2004-08-14 02:36:33")
Time <- as.POSIXct(Time)
x <- data.frame(main.name="AAA", fname=rep(c("Apple","Watermelon"),
                                 each=5), x.name=Time, y.name=(1:10))
par(mai=c(1, .7, .5, .3))
plot(x$x.name, as.character(x$y.name), xlab="Process Time", xaxt='n',
     pch=2)
axis.POSIXct(1, at=seq(min(x$x.name), max(x$x.name), "days"),
             format="%d/%m")
fruit.class <- table(x$fname)
fcolor <- c(611,552,656,121,451,481,28,652,32,550,90,401,150,12,520,8)
for(j in 1:length(fruit.class))
{
    fruit <- names(fruit.class)[j]
    lines(smooth.spline(x[x$fname==fruit, "x.name"],
                        x[x$fname==fruit, "y.name"]),
          col=colors()[fcolor[j]], cex = 0.5, lwd=5)
}
dev.off()

http://rattle.togaware.com/code/rplot-date.R



Subsections

Copyright © 2004-2006 [email protected]
Support further development through the purchase of the PDF version of the book.
Brought to you by Togaware.