r - 从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
我该怎么做呢?
提前致谢
解决方案
像这样的东西:
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)
推荐阅读
- c# - 使页面无法从 URL 访问,只能通过单击页面上的链接访问
- c - 我自己实现 strlcpy 的问题?
- quarkus - Qurkus:如何在 application.yaml 中引用系统“主机名”
- tableau-api - Tableau 删除行
- node.js - 一个查询中的 node-oracledb 多个语句
- java - JFreeChart XY 图中图标的角度
- c++ - 有没有办法使用 c/c++ 中的宏/模板生成具有迭代编号的函数名称?
- flutter - 在自定义挂钩中使用riverpod
- json - 如何优化此 JSON Script VBA 代码以使其不会在 Excel 2019 上崩溃
- node.js - 如何使用因子捆绑修复错误 catlistener