1

I create a time series dataframe that I will use to merge other time series data into.

dates2010 <- seq(as.POSIXct("2010-06-15 00:00:00", tz = "GMT"), as.POSIXct("2010-09-15 23:00:00", tz = "GMT"), by="hour")  # make string of DateTimes for summer 2010
dates2011 <- seq(as.POSIXct("2011-06-15 00:00:00", tz = "GMT"), as.POSIXct("2011-09-15 23:00:00", tz = "GMT"), by="hour")  # make string of DateTimes for summer 2011
dates <- c(dates2010, dates2011)                           # combine the dates from both years
sites <- c("a", "b", "c")                                  # make string of all sites
datereps <- rep(dates, length(sites))                      # repeat the date string the same number of times as there are sites
sitereps <- rep(sites, each = length(dates))               # repeat each site in the string the same number of times as there are dates 
n <- data.frame(DateTime = datereps, SiteName = sitereps)  # merge two strings with all dates and all sites
n <- n[order(n$SiteName, n$Date),]                         # re-order based on site, then date

If I run the above code, 'dates2010' and 'dates2011' are in GMT format: dates2010[1] "2011-06-15 00:00:00 GMT". But when I create the object 'dates' for some reason format switches to EST: dates[1] "2010-06-14 19:00:00 EST"

Maybe it has something to do with POSIX classes?

class(dates2010)
[1] "POSIXct" "POSIXt"

I attempted to change the default time zone for R to GMT to avoid time zone switching problems. This results in an NA coersion error when I attempt to order the data frame 'n' and merge other data frames into 'n'.

n <- n[order(n$SiteName, n$Date),]
Warning message:
In xtfrm.POSIXct(x) : NAs introduced by coercion

Any thoughts on how I might keep time zones constant and avoid the NA coercion errors? Thank You!

Community
  • 1
  • 1
notacodr
  • 129
  • 2
  • 13
  • 3
    `?DateTimeClasses`: _Using `c` on "POSIXlt" objects converts them to the current time zone, and on "POSIXct" objects drops any "tzone" attributes (even if they are all marked with the same time zone)._ Apparently there's no way to avoid it, so you just have to reset the attribute after the fact as shown below. – alistaire Jan 27 '17 at 18:41
  • Your method worked @Rich Scriven to change 'dates' time zone to "GMT". `dates <- structure(c(dates2010, dates2011), tzone = "GMT")`. Thanks. – notacodr Jan 27 '17 at 18:48
  • Oh wow. Thank you for that information @alistaire. – notacodr Jan 27 '17 at 18:49
  • Also, if you are bad at remembering attribute names or just like lubridate, its `with_tz` function does the same thing: `dates <- lubridate::with_tz(c(dates2010, dates2011), 'GMT')` – alistaire Jan 27 '17 at 19:02

1 Answers1

5

c() drops attributes. So when you created dates, the time zone was dropped and it automatically defaulted to the current locale. Fortunately you can use structure() and set the time zone there.

dates <- structure(c(dates2010, dates2011), tzone = "GMT")
head(dates)
# [1] "2010-06-15 00:00:00 GMT" "2010-06-15 01:00:00 GMT" 
# [3] "2010-06-15 02:00:00 GMT" "2010-06-15 03:00:00 GMT"
# [5] "2010-06-15 04:00:00 GMT" "2010-06-15 05:00:00 GMT"

If dates was already created, you can add/change the tzone attribute later.

attr(dates, "tzone") <- "GMT"
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245