Sum last 52 elements without loop
Say I have a data set that has x-sections of country (US, CANADA) and then state/province then year then week. My data stack has 2 countries, 57 states/provinces, 3 years, and 52 weeks. I wanto to create a variable reven开发者_开发知识库ue that for each week, sums the last 52 weeks within the x-section.
Right now i have a loop but its very, very slow.
for each countries, for each state, for the last 2 years, for each week, sum the last 52 elements
Does anyone know how I can do this with vectorization?
You might want to look into using the function s=sum(X,DIM). Without more info about your dataset (please provide example), we cannot go into great detail.
For weighted sums over rolling window, filter() can work well.
精彩评论