[R] chron objects: input/output

Gabor Grothendieck ggrothendieck at gmail.com
Fri Aug 1 19:09:34 CEST 2008


Just recently the ability to handle POSIXt style formats in as.chron
was added:

> x <- c("07/01/2001 12:00:00","07/17/2001 15:00:00")

> y <- as.chron(x, "%m/%d/%Y %H:%M:%S")
> y
[1] (07/01/01 12:00:00) (07/17/01 15:00:00)

> # Here is a workaround

> format(as.POSIXct(y), tz = "GMT", format = "%m/%d %H:%M")
[1] "07/01 12:00" "07/17 15:00"



On Fri, Aug 1, 2008 at 9:48 AM, Stephen Tucker <brown_emu at yahoo.com> wrote:
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list