首页 > 解决方案 > 如何在R中取上一年4月到7月的平均值?

问题描述

Month Year Rainfall
4     2010
5     2010
6     2010
7     2010
8     2010
9     2010
10    2010
11    2010
12    2010
1     2011
2     2011
3      2011
4     2011
5     2011
6     2011
7     2011

我想从 2010 年 4 月到 2011 年 7 月 7 日获取平均值,然后从 2011 年 4 月到 2012 年 7 月 7 日开始获取平均值?

我已经尝试过这段代码,但它只适用于第一部分,所以有人可以在第二部分帮助我吗?

## The code
subdataLGSP<-
  subset(df2.ppt.mon, (Year %in% c(2010,2011,2012,2013,2014,2015,2016)) & (month %in% c(4,5,6,7,8,9,10,11,12))) #Apr from previous year tp July 
Subdatanext<-
  subset(df2.ppt.mon, (Year %in% c(2011,2012,2013,2014,2015,2016)) & (month %in% c(1,2,3,4,5,6,7))) # Apr from previous year to next July 

subdataprnext<-
  rbind(subdataLGSP,Subdatanext)

df2prnext<-
  aggregate(subdataprnext$RAIN, by = list(month = subdataprnext$month, Year= subdataprnext$Year), mean)

library(data.table)
setDT(df2prnext)
n <- 16 # every 16 rows
datPRApOct<-
  df2prnext[, mean(x), by= (seq(nrow(df2prnext)) - 1) %/% n]# This is what we want for seasonal precipitation

标签: rmean

解决方案


像这样的东西会起作用:

一行来创建分组,其余的是标准的R东西

df$gp<- sapply(1:nrow(df), function(x) x%/%12)

我们一起拥有:

library(dplyr)

df <- structure(list(Month = c(4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
                               1L, 2L, 3L, 4L, 5L, 6L, 7L), Year = c(2010L, 2010L, 2010L, 2010L, 
                                                                     2010L, 2010L, 2010L, 2010L, 2010L, 2011L, 2011L, 2011L, 2011L, 
                                                                     2011L, 2011L, 2011L), Rainfall = c(3L, 4L, 5L, 3L, 4L, 5L, 6L, 
                                                                                                        7L, 8L, 4L, 3L, 4L, 5L, 6L, 5L, 4L)), row.names = c(NA, -16L), class = c("data.table", 
                                                                                                                                                                                 "data.frame"))

df
#>    Month Year Rainfall
#> 1      4 2010        3
#> 2      5 2010        4
#> 3      6 2010        5
#> 4      7 2010        3
#> 5      8 2010        4
#> 6      9 2010        5
#> 7     10 2010        6
#> 8     11 2010        7
#> 9     12 2010        8
#> 10     1 2011        4
#> 11     2 2011        3
#> 12     3 2011        4
#> 13     4 2011        5
#> 14     5 2011        6
#> 15     6 2011        5
#> 16     7 2011        4

df$gp<- sapply(1:nrow(df), function(x) x%/%12)

df
#>    Month Year Rainfall gp
#> 1      4 2010        3  0
#> 2      5 2010        4  0
#> 3      6 2010        5  0
#> 4      7 2010        3  0
#> 5      8 2010        4  0
#> 6      9 2010        5  0
#> 7     10 2010        6  0
#> 8     11 2010        7  0
#> 9     12 2010        8  0
#> 10     1 2011        4  0
#> 11     2 2011        3  0
#> 12     3 2011        4  1
#> 13     4 2011        5  1
#> 14     5 2011        6  1
#> 15     6 2011        5  1
#> 16     7 2011        4  1

df %>% group_by(gp) %>% summarise(mean(Rainfall))
#> # A tibble: 2 x 2
#>      gp `mean(Rainfall)`
#>   <dbl>            <dbl>
#> 1     0             4.73
#> 2     1             4.8

lubridate可以说有更好的方法可以使用包或转换为ts对象来处理这个窗口问题。


推荐阅读