0

I've run into a trouble working with time series & zones in R, and I can't quite figure out how to proceed.

I have an time series data like this:

df <- data.frame( 
  Date     = seq(as.POSIXct("2014-01-01 00:00:00"), length.out = 1000, by = "hours"),
  price    = runif(1000, min = -10, max = 125),
  wind     = runif(1000, min = 0, max = 2500),
  temp     = runif(1000, min = - 10, max = 25)  
)

Now, the Date is in UTC-time. I would like to subset/filter the data, so for example I get the values from today (Today is 2014-05-13):

df[ as.Date(df$Date) == Sys.Date(), ]

However, when I do this, I get data that starts with:

2014-05-13 02:00:00

And not:

2014-05-13 00:00:00

Because im currently in CEST-time, which is two hours after UTC-time. So I try to change the data:

df$Date <- as.POSIXct(df$Date, format = "%Y-%m-%d %H", tz = "Europe/Berlin")

Yet this doesn't work. I've tried various variations, such as stripping it to character, and then converting and so on, but I've run my head against a wall, and Im guessing there is something simple im missing.

Thorst
  • 1,590
  • 1
  • 21
  • 35
  • Check out this thread: https://stat.ethz.ch/pipermail/r-help/2006-March/101179.html – Gary Weissman May 13 '14 at 08:03
  • I've tried reading that link and the material about TZ, but am still having trouble figuring how exactly to convert the series. All I can see is that it is system-dependent on what works? I've solved it so far by using a : `Sys.setenv(TZ='GMT')` from [This SO answer](http://stackoverflow.com/questions/6374874/how-to-change-the-default-timezone-in-r) – Thorst May 13 '14 at 09:08
  • 1
    How about using `df[format(df$Date,"%Y-%m-%d") == Sys.Date(), ]` – James May 13 '14 at 09:45
  • Another issue: Your example doesn't have any dates after the 11th Feb. – James May 13 '14 at 09:48
  • @James, your suggestion worked. – Thorst May 13 '14 at 10:31

1 Answers1

2

To avoid using issues with timezones like this, use format to get the character representation of the date:

df[format(df$Date,"%Y-%m-%d") == Sys.Date(), ]
James
  • 65,548
  • 14
  • 155
  • 193