-2

I have a dataset having monthly observations (from January to December) over a period of time like (2000 to 2018). Now, I am trying to take an average the value from March to July of each year.

Reproducible Example:

zz <- "Year    Month  Value
       2000    1        25
       2000    2        28 
       2000    3        22
       2000    12       26
       2001     1       27
       2018    11       30
       2018    12       29"
df <- read.table(text = zz, header = TRUE)

Created on 2020-08-03 by the reprex package (v0.3.0)

Can someone help me in this regard?

Many thanks in advance.

iamericfletcher
  • 2,529
  • 6
  • 17
  • Welcome to Stack Overflow Independet Researcher! Please take some time to review the following post: [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Also, [https://stackoverflow.com/help/someone-answers](https://stackoverflow.com/help/someone-answers). – iamericfletcher Aug 03 '20 at 12:21

1 Answers1

1
library(dplyr)

data <- data.frame(
  year = rep(c(2000, 2001, 2002), each = 12),
  month = rep(1:12, times = 3),
  value = sample(1:1000, 3*12)
)

data.from.march.to.july <- data[data$month %in% 3:7, ]

data.averages <- data.from.march.to.july %>%
  group_by(year) %>%
  summarise(avg = mean(value), .groups = "drop_last")