[R] Date/time

Gabor Grothendieck ggrothendieck at gmail.com
Sat Jan 14 16:37:35 CET 2012


On Sat, Jan 14, 2012 at 4:35 AM, claire5 <claire.morandin at free.fr> wrote:
> I have been trying for some time to nicely plot some of my data, I have
> around 1800 values for some light intensity, taken every hour of the day
> over almost 2 months.
>
> My data file looks like:
>
>     Date    Time. GMT.02.00 Intensity
> 1    06.10.11 11:00:00        AM   x
> 2    06.10.11 12:00:00        PM   x
> 3    06.10.11 01:00:00        PM   x
> 4    06.10.11 02:00:00        PM     x
>
> As I am pretty new to R, I am totally struggling with this issue, does
> anyone has an idea on how I could plot nicely the data and if I need to
> change my data file?
>

With the zoo package its as follows.  For the actual data which
resides in a file rather than in a character string, Lines, we would
replace text=Lines with something like "myfile.dat".

# sample data
Lines <- "Date    Time. GMT.02.00 Intensity
1    06.10.11 11:00:00        AM   1
2    06.10.11 12:00:00        PM   2
3    06.10.11 01:00:00        PM   3
4    06.10.11 02:00:00        PM   4"

library(zoo)
z <- read.zoo(text = Lines, index = 1:3, tz = "", format = "%m.%d.%y %r")
plot(z)


We might alternately want to use chron date/times to avoid time zone
problems later (as per R News 4/1).  In that case it would be:

library(zoo)
library(chron)
toChron <- function(d, t, p) as.chron(paste(d, t, p), format = "%m.%d.%y %r")
z <- read.zoo(text = Lines, index = 1:3, FUN = toChron)
plot(z)

Note that in both cases we could omit header = TRUE because there is
one more data column than header column so it can deduce the correct
header= value.

Read the 5 zoo vignettes and particularly the one on read.zoo as well
as the help files for more info.


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list