Rolling window function for irregular time series that can handle duplicates
问题 I have the following data.frame: grp nr yr 1: A 1.0 2009 2: A 2.0 2009 3: A 1.5 2009 4: A 1.0 2010 5: B 3.0 2009 6: B 2.0 2010 7: B NA 2011 8: C 3.0 2014 9: C 3.0 2019 10: C 3.0 2020 11: C 4.0 2021 Desired output: grp nr yr nr_roll_period_3 1 A 1.0 2009 NA 2 A 2.0 2009 NA 3 A 1.5 2009 NA 4 A 1.0 2010 NA 5 B 3.0 2009 NA 6 B 2.0 2010 NA 7 B NA 2011 NA 8 C 3.0 2014 NA 9 C 3.0 2019 NA 10 C 3.0 2020 NA 11 C 4.0 2021 3.333333 The logic: I want to calculate a rolling mean for the period of length k