Sebastian P. Luque
2006-Sep-27 20:12 UTC
[R] how to retain time zone when doing c(POSIXct)
Hello,
What is the best way to concatenate POSIXct objects keeping the time zone
attribute in a program? For example:
R> xx <- as.POSIXct(strptime(c("2006-09-26 12:00:00",
"2006-09-26 13:00:00"),
+ format="%Y-%m-%d %H:%M:%S"),
tz="GMT")
R> xx
[1] "2006-09-26 12:00:00 GMT" "2006-09-26 13:00:00 GMT"
but, as ?c.POSIXct explains:
R> c(xx, xx[1] - 60, xx[2] + 60)
[1] "2006-09-26 07:00:00 CDT" "2006-09-26 08:00:00 CDT"
[3] "2006-09-26 06:59:00 CDT" "2006-09-26 08:01:00 CDT"
Is there something better/safer than simply setting the "tzone"
attribute
of the new object?
Cheers,
--
Seb
Gabor Grothendieck
2006-Sep-28 10:23 UTC
[R] how to retain time zone when doing c(POSIXct)
Here is one way that does not require explicit assignment of tzone: rbind(data.frame(x=xx), data.frame(x=xx[1] - 60), data.frame(x=xx[2] + 60))[[1]] With xx from the post we get:> rbind(data.frame(x=xx), data.frame(x=xx[1] - 60), data.frame(x=xx[2] + 60))[[1]][1] "2006-09-26 12:00:00 GMT" "2006-09-26 13:00:00 GMT" [3] "2006-09-26 11:59:00 GMT" "2006-09-26 13:01:00 GMT" On 9/27/06, Sebastian P. Luque <spluque at gmail.com> wrote:> Hello, > > What is the best way to concatenate POSIXct objects keeping the time zone > attribute in a program? For example: > > > R> xx <- as.POSIXct(strptime(c("2006-09-26 12:00:00", "2006-09-26 13:00:00"), > + format="%Y-%m-%d %H:%M:%S"), tz="GMT") > R> xx > [1] "2006-09-26 12:00:00 GMT" "2006-09-26 13:00:00 GMT" > > > but, as ?c.POSIXct explains: > > > R> c(xx, xx[1] - 60, xx[2] + 60) > [1] "2006-09-26 07:00:00 CDT" "2006-09-26 08:00:00 CDT" > [3] "2006-09-26 06:59:00 CDT" "2006-09-26 08:01:00 CDT" > > > Is there something better/safer than simply setting the "tzone" attribute > of the new object? > > > Cheers, > > -- > Seb > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >