[R] chron objects: input/output

Stephen Tucker brown_emu at yahoo.com
Fri Aug 1 15:48:17 CEST 2008


Hi list, I have some questions regarding

1) conversion of date + time characters to chron
2) formatting chron object printing

Regarding (1), Gabor's Rnews 2004 4/1 article has been indispensible,
but I often work with files where dates and times are contained in a
single field. In this case, I would like to control input/output of
chron objects when each observation of date and time is stored as a
single string.

So here are some procedures that I've tried:
## define character vector
> x <- c("07/01/2001 12:00:00","07/17/2001 15:00:00")
## method 1
> library(chron)
> chron(substring(x,1,10),substring(x,12))
[1] (07/01/01 12:00:00) (07/17/01 15:00:00)
## method 2 (recently inspired by Gabor's post)
> do.call(c,strapply(x,"(.*) (.*)",chron,backref=-2))
[1] (07/01/01 12:00:00) (07/17/01 15:00:00)
## method 3
> (chronObj <- as.chron(strptime(x,"%m/%d/%Y %T")))
[1] (07/01/01 12:00:00) (07/17/01 15:00:00)

Could there be any gotchas with the third method
(as.chron(strptime(...)))?  The 'tz' attribute for POSIXlt objects are
ignored, but I am not sure if there are any implications of the
'$isdst' field are for conversions. I do like this alternative for the
conciseness-flexibility tradeoff; in my experience, I have not had any
problems - but wanted to inquire if at some point the '$isdst' field (or
possibly something else) could give me trouble.

Regarding the printing of chron objects, this behavior is peculiar to me:
> format(chronObj,format=c("m/d","h:m"))
[1] "(0701 1200)" "(0717 1500)"

The special characters ("/",":") are not printed - I've tried changing
the attribute of the chron object, looked at the format.chron() method
(getS3method("format","chron")), etc. and am still confused; the
chron() documentation says its specification should be similar the
input format. Could I have missed something?

Thanks!

ST



More information about the R-help mailing list