Get Started

Generating a monthly average

While the to.period() command is useful in many contexts, for your purposes it may not be useful to select a single row as representative of the entire month.

Instead, it makes more sense to generate average temperature values per month. To do so, you'll need to manually calculate the monthly average using split() and lapply(), then generate a new xts object using as.xts() This may seem like a complicated process, but you already have the skills to do it!

The subsetted xts object from the previous exercise, temps_xts_2, is preloaded in your workspace. Also preloaded is an index object containing a vector of dates for the first day of each month covered in the data.

This is a part of the course

“Case Study: Analyzing City Time Series Data in R”

View Course

Exercise instructions

  • Use split() to generate monthly lists from the mean column of your temps_xts_2 object. Be sure to specify "months" as the period (the f argument).
  • Use lapply() to calculate the "mean of means", or the average mean temperature per month.
  • Use as.xts() to generate a new xts object containing average monthly temperature in Boston from 2010 through 2015. To do this, you will need to combine your monthly mean_of_means data with your monthly index object.
  • Finally, confirm that your new temps_monthly object shares a duration and periodicity with flights_xts.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Split temps_xts_2 into separate lists per month
monthly_split <- split(___$___ , f = "___")

# Use lapply to generate the monthly mean of mean temperatures
mean_of_means <- lapply(___, FUN = ___)

# Use as.xts to generate an xts object of average monthly temperature data
temps_monthly <- as.xts(as.numeric(___), order.by = ___)
 
# Compare the periodicity and duration of your new temps_monthly and flights_xts 
periodicity(___)
periodicity(___)
Edit and Run Code

This exercise is part of the course

Case Study: Analyzing City Time Series Data in R

IntermediateSkill Level
5.0+
2 reviews

Strengthen your knowledge of the topics covered in Manipulating Time Series in R using real case study data.

In this chapter, you'll expand your time series data library to include weather data in the Boston area. Before you can conduct any analysis, you'll need to do some data manipulation, including merging multiple xts objects and isolating certain periods of the data. It's a great opportunity for more practice!

Exercise 1: Merging time series data by rowExercise 2: Exploring temperature dataExercise 3: Next steps - IExercise 4: Merging using rbind()Exercise 5: Visualizing Boston wintersExercise 6: Merging time series data by columnExercise 7: Subsetting and adjusting periodicityExercise 8: Generating a monthly average
Exercise 9: Using merge() and plotting over timeExercise 10: Are flight delays related to temperature?Exercise 11: Time series data workflowExercise 12: Next steps - IIExercise 13: Expanding your dataExercise 14: Are flight delays related to visibility or wind?

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free