1. Learn
  2. /
  3. Courses
  4. /
  5. Manipulating Time Series Data with xts and zoo in R

Exercise

Using lapply() and split() to apply functions on intervals

Along the same lines as the previous exercise, xts gives you an additional mechanism to dive into periods of your data. Often it is useful to physically split your data into disjoint chunks by time and perform some calculation on these periods.

For this exercise you'll make use of the xts split() command to chunk your data by time. The split() function creates a list containing an element for each split. The f argument in split() is a character string describing the period to split by (i.e. "months", "years", etc.).

Here you will follow the same process you followed in the previous exercise. However, this time you will manually split your data first, and then apply the mean() function to each chunk. The function lapply() is used for the most efficient calculations. In cases where you don't want to return a time series, this proves to be very intuitive and effective.

Instructions

100 XP
  • Use split() to split your temps data by weeks. Call this temps_weekly
  • Use lapply() to get the weekly mean of temps_weekly. Call this temps_avg. Print this list.