Exercise

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.

Instructions 1/3

undefined XP
    1
    2
    3

Question

The mean of a dataset is defined by the total of all observations, divided by the number of observations. What two R functions do you need to calculate the mean 'by hand'? The daily_temp time series is available to you.

Possible answers