请参阅lubridate
使使用日期/时间更容易的包。
对于您的问题,我想您可以使用sapply
:
df["season"] = sapply(df["date"], assign_season)
其中,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")]
}
现在,拆分列表应该包含您需要的所有数据