Changing time zones with POSIXct time series, R

喜你入骨 提交于 2019-12-23 05:37:06

问题


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.


回答1:


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(), ]


来源:https://stackoverflow.com/questions/23625735/changing-time-zones-with-posixct-time-series-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!