首页 > 解决方案 > 从R中的时间序列中提取某些月份

问题描述

我正在处理气候数据,并有一个从 1974 年到 2012 年的每日温度测量时间序列。我想将我的时间序列分成 2 个时间序列,一个是生长季节(每年 4 月 1 日至 8 月 31 日),另一个是非- 生长季节(每年 9 月 1 日至 3 月 31 日)。

我的数据看起来像这样:

[1] 98.9 98.7 100.9 102.4 105.1 106.7 105.9 104.6 103.9 103.8 107.0 108.4 108.0 107.4 110.6 107.3 [17] 106.2 108.7 108.2 106.5 102.3 102.1 107.5 106.5 105.7 103.7 104.8 105.8 105.1 104.6 106.1 106.6 [33] 106.0 103.3 103.1 104.4 102.9 103.0 104.0 109.8 110.5 111.5 107.9 106.1 104.9 105.7 107.1 104.8 [49] 105.7 103.5 101.4 103.1 105.7 105.7 101.7 100.7 102.1 101.4 101.9 101.9 101.4 101.4 101.2 101.2

我该怎么做呢?

提前致谢

标签: rfiltertime-series

解决方案


像这样的东西:

library(tibble)
library(dplyr)
library(lubridate)

climate <- tibble(
  temperature = c(98.9, 98.7, 100.9, 102.4, 105.1, 106.7, 105.9, 104.6, 103.9, 103.8, 107.0, 108.4, 108.0, 107.4, 110.6, 107.3, 106.2, 108.7, 108.2, 106.5, 102.3, 102.1, 107.5, 106.5, 105.7, 103.7, 104.8, 105.8, 105.1, 104.6, 106.1, 106.6, 106.0, 103.3, 103.1, 104.4, 102.9, 103.0, 104.0, 109.8, 110.5, 111.5, 107.9, 106.1, 104.9, 105.7, 107.1, 104.8, 105.7, 103.5, 101.4, 103.1, 105.7, 105.7, 101.7, 100.7, 102.1, 101.4, 101.9, 101.9, 101.4, 101.4, 101.2, 101.2),
  date = seq(as.Date("1974-01-01"), by="1 day", l = length(temperature)),
  month = month(date),
  growing_season = (month %in% (4:8))
)

growing <- climate %>% filter(growing_season)
non_growing <- climate %>% filter(!growing_season)

推荐阅读