问题描述
限时送ChatGPT账号..我在处理时间序列时遇到了麻烦 &R 中的区域,我无法弄清楚如何继续.
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)
)
现在,日期是 UTC 时间.我想对数据进行子集/过滤,例如我从今天(今天是 2014-05-13)获取值:
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
而不是:
2014-05-13 00:00:00
因为我目前处于 CEST 时间,即 UTC 时间后两小时.所以我尝试更改数据:
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.
推荐答案
为了避免使用时区这样的问题,使用 format
来获取日期的字符表示:
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(), ]
这篇关于使用 POSIXct 时间序列更改时区,R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论