![](/img/trans.png)
[英]How to select specific date range repeating over multiple years in R?
[英]How to find the average from a fixed date range for multiple years in R
我的数据集包含从 1967 年到 2022 年一年中每一天的河流流量测量值。我想找到从 4 月开始和结束的年度平均流量(例如,从 1967 年 4 月到 1969 年 4 月的平均流量, 1968 年 4 月 - 1969 年 4 月,1969 年 4 月 - 1970 年 4 月等)。
这是我的数据示例:
A tibble: 20,100 x 7
river year season month date flow_rate quality
<chr> <fct> <fct> <fct> <date> <dbl> <chr>
1 wylye 1967 Winter January 1967-01-01 6.67 Good
2 wylye 1967 Winter January 1967-01-02 6.39 Good
3 wylye 1967 Winter January 1967-01-03 6.32 Good
4 wylye 1967 Winter January 1967-01-04 6.34 Good
5 wylye 1967 Winter January 1967-01-05 6.37 Good
6 wylye 1967 Winter January 1967-01-06 6.45 Good
7 wylye 1967 Winter January 1967-01-07 6.65 Good
8 wylye 1967 Winter January 1967-01-08 6.54 Good
9 wylye 1967 Winter January 1967-01-09 6.53 Good
10 wylye 1967 Winter January 1967-01-10 6.62 Good
# ... with 20,090 more rows
我见过人们在同一年(7 月至 10 月)找到某些月份的平均值的代码
例如
df %>%
mutate(date = as.Date(date),
day = day(date),
month = month(date),
year = year(date)) %>%
filter(between(month, 7, 10) |
day >= 7 & month == 6 |
day <= 9 & month == 11) %>%
group_by(year) %>%
summarise(tmax = mean(tmax, na.rm = TRUE))
但不是一个允许我查看多年平均数的代码(1967-1968/1968-1969/1969-1970 等)。 任何帮助,将不胜感激:)
这是使用lubridate
的一种方法,方法是移动年份以匹配您的时间间隔
df<-tibble::tribble(
~date, ~flow_rate,
"1967-01-01", 1,
"1967-04-01", 2,
"1968-01-01", 3,
"1968-04-01", 4 )
library(dplyr)
library(lubridate)
df_new<-df %>%
mutate(date=ymd(date),
year_shift=year(date-days(90)),
label=paste("April",year_shift,"-","March",year_shift+1)) %>%
group_by(label) %>%
summarize(flow_rate = mean(flow_rate)) %>%
ungroup()
df_new
#> # A tibble: 3 × 2
#> label flow_rate
#> <chr> <dbl>
#> 1 April 1966 - March 1967 1
#> 2 April 1967 - March 1968 2.5
#> 3 April 1968 - March 1969 4
由代表 package (v2.0.1) 于 2022 年 1 月 25 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.