Split-Apply-Combine
A common analytical pattern is to:
- split data into pieces,
- apply some function to each piece,
- combine the results back together again.
Generally avoid using loops when you need to do Split-Apply-Combine, consider these alternatives instead:
- Entry level:
dplyr::group_by()
- General approach: nesting
*aplly
functions and plyr
package (non-tidyverse solution)
Exercise
- Fit linear regression models of the daily bike counts on percipitation and max temperature, first for both bridges together and then for each bridge separately using the split-apply-combine pattern;
- When using ggfortify to plot weekly variation, trend and noise separately, you need to plot each bridge separately (sample code here). Use the split-apply-combine to avoid having to repeat for each bridge.