Rolling apply with a custom function
The rollapply
function from zoo
allows you to apply any summary function to a rolling window. A summary function takes multiple numbers as input and returns a single number, like mean()
, max()
, sum()
, etc.
Depending on the kind of data you have, you might need to write a function that combines multiple statistics, like in the example of our find_range()
function in the video exercise.
In this exercise, you'll create a brief function that 'manually' calculates the mean value of a range of numbers, and apply it to a rolling window.
The dataset you'll work with, daily_temp
, and the zoo
and ggplot2
packages, are loaded for you.
This exercise is part of the course
Manipulating Time Series Data in R
Hands-on interactive exercise
Turn theory into action with one of our interactive exercises
