Thank, Ballardw. I understand how lag function works and actually used it in one of my project to create lagged sampling windows. This question here came from that same project. Right now, the data I have is perfect, without any broken time period/missing period. Therefore, my code works fine. However, I did see some times there will be missing time periods in certain data. And if that happens, my code won't work. Thus, I am trying to make my code robust (or ready) to this kind of situations. After observations are assigned to certain groups (G1-G5), an average of 'value' for each group will be calculated. An averges trend will be plotted after that. Notice that G3, G4, and G5 will only have two obs due to missing data. I hope this make sense. Thanks again. Joe
... View more