Vishnupriya
Vishnupriya

Reputation: 49

Mean of few months for a monthly data in r

I want to find the average of the months from Nov to March, say Nov 1982 to Mar 1983. Then, for my result, I want a column with year and mean in another. If the mean is taken till Mar 1983, I want the year to be shown as 1983 along with that mean. This is how my data looks like.

I want my result to look like this.

1983 29.108
1984 26.012

I am not very good with R packages, If there is an easy way to do this. I would really appreciate any help. Thank you.

Upvotes: 0

Views: 640

Answers (3)

Ronak Shah
Ronak Shah

Reputation: 389325

Here is one approach to get average of Nov-March every year.

library(dplyr)

df %>%
  #Remove data for month April-October
  filter(!between(month, 4, 10)) %>%
  #arrange the data by year and month
  arrange(year, month) %>%
  #Remove 1st 3 months of the first year and 
  #last 2 months of last year
  filter(!(year == min(year) & month %in% 1:3 | 
         year == max(year) & month %in% 11:12)) %>%
  #Create a group column for every November entry
  group_by(grp = cumsum(month == 11)) %>%
  #Take average for each year 
  summarise(year = last(year),
            value = mean(value)) %>%
  select(-grp)

# A tibble: 2 x 2
#   year  value
#  <int>  <dbl>
#1  1982  0.308
#2  1983 -0.646

data

It is easier to help if you provide data in a reproducible format which can be copied easily.

set.seed(123)
df <- data.frame(year = rep(1981:1983, each = 12),month = 1:12,value = rnorm(36))

Upvotes: 2

Macosso
Macosso

Reputation: 1439

With dplyr

# remove the "#" before in the begining of the next line if dplyr or tidyverse is not installed
#install.packages("dplyr") 

library(dplyr) #reading the library

colnames(df) <- c("year","month","value") #here I assumed your dataset is named df


df<- df%>%
  group_by(year) %>%
  summarize(av_value =mean(value))

Upvotes: 2

Rentrop
Rentrop

Reputation: 21507

You can do this as follow using tidyverse

require(tidyverse)

year <- rep(1982:1984, 3)
month <- rep(1:12, 3)
value <- runif(length(month))

dat <- data.frame(year, month, value)

head(dat)

dat looks like your data

# A tibble: 3 × 2
   year value
  <int> <dbl>
1  1982 0.450
2  1983 0.574
3  1984 0.398

The trick then is to group_by and summarise

dat %>% 
  group_by(year) %>% 
  summarise(value = mean(value))

Which gives you

# A tibble: 3 × 2
   year value
  <int> <dbl>
1  1982 0.450
2  1983 0.574
3  1984 0.398

Upvotes: 1

Related Questions