R:将时间序列拆分为自定义季节

发布于 2025-01-10 18:57:10 字数 450 浏览 0 评论 0原文

我有一个时间序列数据框:

[https://www.dropbox.com/s/elaxfuvqyip1eq8/SampleDF.csv?dl=0][1]

我的目的是根据以下条件将该数据框划分为不同的季节:

  1. 冬季:12月1月2月
  2. 季风前:3月4月5月6月15日(即直到6月15日)
  3. 季风:15Jun Jul 8 月 9 月(即从 6 月 15 日开始)
  4. 季风后:10 月 11 月。

我尝试使用 openair 包函数

selectByDate()

但还没有运气。 作为 R 新手。 任何帮助将不胜感激。

谢谢!

I have a time Series DataFrame:

[https://www.dropbox.com/s/elaxfuvqyip1eq8/SampleDF.csv?dl=0][1]

My intention is to divide this DataFrame into different seasons according to:

  1. winter: Dec Jan Feb
  2. Pre-monsoon: Mar Apr May Jun15 (i.e. till 15th of June)
  3. Monsoon: 15Jun Jul Aug Sep (i.e. from 15th of June)
  4. Post-monsoon: Oct Nov.

I tried using openair package function

selectByDate()

But no luck yet.
Being novice in R.
Any help would be highly appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽手叙旧 2025-01-17 18:57:10

请参阅 lubridate 包,它使日期/时间的处理变得更加容易。

对于你的问题,我想你可以使用 sapply:

df["season"] = sapply(df["date"], assign_season)

where, assign_season:

assign_season <- function(date){
    # return a season based on date 

}

一旦你有季节,那么你可以轻松地划分数据帧:

winter = subset(df, season == "winter")
# and so on

抱歉,我现在必须赶时间,但如果其他人还没有回答,可以回来完成这个。

编辑:

所以,R 确实有一个内置函数 cut,它可以处理日期并根据日期范围分割向量。

对于您的数据,我这样做了:

library(lubridate)
library(dplyr)

df = read.csv('SampleDF.csv')

## reformat date into POSIXct
df <- df %>%
         mutate(date_reformat = as.POSIXct(date(mdy_hm(date))))

## define breaks & labels
breaks = c("2014-12-01", "2015-03-01", "2015-06-15", "2015-10-01", "2015-12-01", "2016-03-01", "2016-06-15", "2016-10-01", "2016-12-01", "2017-03-01")
labels = c("winter", "pre_monsoon", "monsoon", "post_monsoon", "winter", "pre_monsoon", "monsoon", "post_monsoon", "winter")
df["season"] = cut(df$date_reformat, breaks=as.POSIXct(breaks), labels=labels)

splits = list()

for (s in c("winter", "pre_monsoon", "monsoon", "post_monsoon")){
  splits[[s]] = subset(df, season == s)[c("date", "value")]
}

现在,拆分列表应该包含您需要的所有数据

Please see the lubridate package which makes working with date/time a bit easier.

For your problem, I guess you can use sapply:

df["season"] = sapply(df["date"], assign_season)

where, assign_season:

assign_season <- function(date){
    # return a season based on date 

}

once you have seasons, then you can divide the dataframe easily:

winter = subset(df, season == "winter")
# and so on

Sorry, I have to rush now, but can come back and finish this, if someone else hasn't answered already.

EDIT:

So, R does have a built in function cut, that can work on dates and split a vector based on date ranges.

For your data, I did this like so:

library(lubridate)
library(dplyr)

df = read.csv('SampleDF.csv')

## reformat date into POSIXct
df <- df %>%
         mutate(date_reformat = as.POSIXct(date(mdy_hm(date))))

## define breaks & labels
breaks = c("2014-12-01", "2015-03-01", "2015-06-15", "2015-10-01", "2015-12-01", "2016-03-01", "2016-06-15", "2016-10-01", "2016-12-01", "2017-03-01")
labels = c("winter", "pre_monsoon", "monsoon", "post_monsoon", "winter", "pre_monsoon", "monsoon", "post_monsoon", "winter")
df["season"] = cut(df$date_reformat, breaks=as.POSIXct(breaks), labels=labels)

splits = list()

for (s in c("winter", "pre_monsoon", "monsoon", "post_monsoon")){
  splits[[s]] = subset(df, season == s)[c("date", "value")]
}

Now, the splits list should have all the data you need

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文