使用 POSIXct 时间序列更改时区,R

编程入门 行业动态 更新时间:2024-10-25 06:26:21
本文介绍了使用 POSIXct 时间序列更改时区,R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-30 10:03:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1391956.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列   时区   时间   POSIXct

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!