R中按月分组的天组数据

时间:2019-05-02 21:15:39

标签: r time-series lubridate

我正在尝试按每个月内的10天时间段来汇总此每日降雨序列,并计算累积降雨。

library(tidyverse)
(dat <- tibble(
  date = seq(as.Date("2016-01-01"), as.Date("2016-12-31"), by=1),
  rainfall = rgamma(length(date), shape=2, scale=2)))

因此,我将获得一年中第三组的可变性,例如:一月的第三期间为11天,二月为9天,依此类推。这是我的尝试:

library(lubridate)
dat %>% 
  group_by(decade=floor_date(date, "10 days")) %>%
  summarize(acum_rainfall=sum(rainfall), 
            days = n())

这是结果输出

# A tibble: 43 x 3
   decade     acum_rainfall  days
   <date>             <dbl> <int>
 1 2016-01-01         48.5     10
 2 2016-01-11         39.9     10
 3 2016-01-21         36.1     10
 4 2016-01-31          1.87     1
 5 2016-02-01         50.6     10
 6 2016-02-11         32.1     10
 7 2016-02-21         22.1      9
 8 2016-03-01         45.9     10
 9 2016-03-11         30.0     10
10 2016-03-21         42.4     10
# ... with 33 more rows

有人可以帮助我将残差期加到第三个残差期,以便每个月始终获得3个期吗?这将是所需的输出(请注意第3行):

   decade     acum_rainfall  days
   <date>             <dbl> <int>
 1 2016-01-01         48.5     10
 2 2016-01-11         39.9     10
 3 2016-01-21         37.97    11
 4 2016-02-01         50.6     10
 5 2016-02-11         32.1     10
 6 2016-02-21         22.1      9

1 个答案:

答案 0 :(得分:2)

执行此操作的一种方法是使用if_elsefloor_date应用于date,具体取决于day(date)的日值。如果'20 days'是<30,请使用常规方法,如果> = 30,则使用dat %>% group_by(decade=if_else(day(date) >= 30, floor_date(date, "20 days"), floor_date(date, "10 days"))) %>% summarize(acum_rainfall=sum(rainfall), days = n()) # A tibble: 36 x 3 decade acum_rainfall days <date> <dbl> <int> 1 2016-01-01 38.8 10 2 2016-01-11 38.4 10 3 2016-01-21 43.4 11 4 2016-02-01 34.4 10 5 2016-02-11 34.8 10 6 2016-02-21 25.3 9 7 2016-03-01 39.6 10 8 2016-03-11 53.9 10 9 2016-03-21 38.1 11 10 2016-04-01 36.6 10 # … with 26 more rows 以确保将其舍入到第21天:

"."