Changing time zones with POSIXct time series, R

2019-06-08 17:23发布

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.

标签: r timezone
1条回答
不美不萌又怎样
2楼-- · 2019-06-08 18:03

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(), ]
查看更多
登录 后发表回答