hope you can help me out with this: I´m currently plotting climate data (see pic for structure) in R. Therefore I´m trying to calculate the yearly average for 45 years based on monthly means. Calculating the yearly mean based on the monthly values is where I´m struggling. Yearly sum (like precipitation) is no problem. Thank you very much for your help
Below you can find the code I used for the total yearly precipitation, which I tried to modify for the mean yearly values.
# Generating precipitation data
set.seed(45) # For reproducibility
precipitation_data <- data.frame(
year = rep(1977:2022, each = 12),
month = rep(1:12, times = 46),
amount_precipitation = runif(46 * 12, min = 0, max = 1000)
)
`# Calculating yearly total precipitation
yearly_precipitation <- precipitation_data %>%
group_by(year) %>%
summarize(TotalPrecipitation = sum(amount_precipitation))
But I don´t know how to calculate the yearly mean
To calculate the yearly average temp I tried this as well, but don´t know how to proceed as I need to focus on the mean (don´t know wether the dplyr package is appropriate as I only have years and no longer dates):
set.seed(45) # For reproducibility
yearly_average_temp <- data.frame(
year = rep(1977:2022, each = 12),
month = rep(1:12, times = 46),
avgTemp = runif(46 * 12, min = 0, max = 1000)`
Thank you very much for your help. Cheers
Taking monthly averages and rejoining by year:
If so, you just need to do it in two steps. First average by year and month, then average those monthly values. You might also check out the tsibble package or fpp3 which has advanced functionality for time series calculations.