UseR10085
UseR10085

Reputation: 8198

How to summarise in particular sequence using dplyr?

I want to sum 11 and 12 month of a particular year with 1, 2, and 3 month of the subsequent year. I am using the following code

library(tidyverse) 
library(seas) 
library(lubridate)

## get mscdata from "seas" packages
data(mscdata)
dat <- (mksub(mscdata, id=1108447))

dat %>% 
  mutate(Year = year(date),
         Month = month(date),
         Day = day(date)) %>% 
  group_by(Year, Month) %>% 
  summarise(Monthly = sum(precip)) %>% 
  subset(Month %in% c(11, 12, 1, 2, 3)) %>% 
  group_by(Year) %>% 
  summarise(try = sum(Monthly)) 

The above code sums up 1, 2, 3, 11 and 12 of each year.

   Year   try
   <dbl> <dbl>
 1  1975  826.
 2  1976  576.
 3  1977  584.
 4  1978  536 
 5  1979  653.
 6  1980  942.
 7  1981  739.
 8  1982  853.
 9  1983  990.
10  1984  927.

My expected output should be

Year   try
1976    850.3
1977    396.6
1978    652.6
1979    508.7
1980    764.4
1981    897.4
1982    912.4
1983    880.3
1984    964.8
1985    620

Upvotes: 2

Views: 227

Answers (1)

Anoushiravan R
Anoushiravan R

Reputation: 21938

You can use the following code to get to your desired output. I first thought of row-binding months 11 & 12 of a year with months 1 to 3 of the following year for then summarizing the precip variable but it got painfully slow in execution. I then decided to sum the first 3 months and last 2 months of every year and then use lag function to do the computation.

library(dplyr)
library(lubridate)
library(seas)

## get mscdata from "seas" packages
data(mscdata)
dat <- (mksub(mscdata, id=1108447))


dat %>%
  mutate(month = month(date)) %>%
  group_by(year, month) %>%
  filter(month %in% c(11, 12, 1, 2, 3)) %>%
  group_by(year) %>%
  summarise(monthly_3 = sum(precip[month %in% c(1, 2, 3)]),
            monthly_2 = sum(precip[month %in% c(11, 12)])) %>%
  mutate(monthly = monthly_3 + lag(monthly_2)) %>%
  select(-c(monthly_3, monthly_2))


# A tibble: 30 x 2
    year monthly
   <int>   <dbl>
 1  1975     NA 
 2  1976    850.
 3  1977    397.
 4  1978    653.
 5  1979    509.
 6  1980    764.
 7  1981    897.
 8  1982    912.
 9  1983    880.
10  1984    965.
# ... with 20 more rows

Upvotes: 1

Related Questions