Easy moving averages

R
time_series
tidy
Author

Robert W. Walker

Published

May 24, 2021

An r-bloggers post

There is a handy post on r-bloggers that details moving averages built around the amazing tidyquant package.

First, get some data.

library(tidyverse)
GlobalLandTemperaturesByMajorCity <- read_csv("data/GlobalLandTemperaturesByMajorCity.csv",
col_types = cols(dt = col_date(format = "%Y-%m-%d")))

Let me choose London.

London.Data <- GlobalLandTemperaturesByMajorCity %>% filter(City=="London")
head(London.Data)
# A tibble: 6 × 7
  dt         AverageTemperature AverageTemperatu…¹ City  Country Latit…² Longi…³
  <date>                  <dbl>              <dbl> <chr> <chr>   <chr>   <chr>  
1 1743-11-01               7.54               1.75 Lond… United… 52.24N  0.00W  
2 1743-12-01              NA                 NA    Lond… United… 52.24N  0.00W  
3 1744-01-01              NA                 NA    Lond… United… 52.24N  0.00W  
4 1744-02-01              NA                 NA    Lond… United… 52.24N  0.00W  
5 1744-03-01              NA                 NA    Lond… United… 52.24N  0.00W  
6 1744-04-01               8.30               2.50 Lond… United… 52.24N  0.00W  
# … with abbreviated variable names ¹​AverageTemperatureUncertainty, ²​Latitude,
#   ³​Longitude