0

I have the following data set

Data <-structure(list(GPStime = structure(c(1581116832, 1581116833, 
1581116834, 1581116835, 1581116836, 1581116837, 1581116838, 1581116839, 
1581116840, 1581116841, 1581116842, 1581116843, 1581116844, 1581116845, 
1581116846, 1581116847, 1581116848, 1581116849, 1581116850, 1581116851, 
1581116852, 1581116853, 1581116854, 1581116855, 1581116856), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), MinimaxVel = c(3.091, 3.425, 4.617, 
4.277, 4.546, 5.396, 4.865, 4.802, 5.158, 4.381, 4.728, 5.394, 
4.346, 4.941, 5.191, 4.493, 5.189, 4.766, 4.671, 5.461, 4.321, 
4.985, 4.739, 4.537, 5.422), GarminVel = c(5.234, 5.207, 5.17358333333333, 
5.14013888888889, 5.10672222222222, 5.07327777777778, 5.03986111111111, 
5.00641666666667, 4.973, 4.861, 5.104, 5.076, 5.374, 4.871, 5.188, 
5.18627777777778, 5.18455555555556, 5.18280555555556, 5.18108333333333, 
5.17936111111111, 5.17763888888889, 5.17591666666667, 5.17419444444444, 
5.17244444444444, 5.17072222222222), VelDiff = c(-2.143, -1.782, 
-0.556583333333334, -0.863138888888889, -0.560722222222222, 0.322722222222223, 
-0.17486111111111, -0.204416666666667, 0.185, -0.48, -0.376, 
0.318000000000001, -1.028, 0.0700000000000003, 0.00300000000000011, 
-0.693277777777777, 0.00444444444444336, -0.416805555555556, 
-0.510083333333333, 0.28163888888889, -0.85663888888889, -0.190916666666666, 
-0.435194444444444, -0.635444444444444, 0.251277777777778)), row.names = c(NA, 
25L), class = "data.frame")

Where I would like to leave the column MinimaxVel as it is, but calculate the mean of GarminVel over different time periods. For example I have a MinimaxVel value and a corresponding GarminVel value. I need a new GarminVel value to to be calculated as mean(GarminVel-1, GarminVel, GarminVel+1). i.e. the time period I am averaging over is 3 seconds, offset by 1 second. Below is the desired outcome for the first 3 sec.

 GPStime MinimaxVel GarminVel   VelDiff
1 2020-02-07 23:07:12      3.091        NA        NA
2 2020-02-07 23:07:13      3.425  5.204861 -1.779861
3 2020-02-07 23:07:14      4.617  5.173574 -0.556574

I would like to do this over different time periods, e.g. 3 - 15 seconds. I anyone able to help with the offsetting? I have looked at lag and rollmean but haven't achieved the results I would like yet

Thank you

wattss
  • 61
  • 5

1 Answers1

1

You can use rollmean here :

transform(Data, GarminVel = zoo::rollmean(GarminVel, 3, fill = NA))

#              GPStime MinimaxVel GarminVel VelDiff
#1 2020-02-07 23:07:12       3.09        NA  -2.143
#2 2020-02-07 23:07:13       3.42      5.20  -1.782
#3 2020-02-07 23:07:14       4.62      5.17  -0.557
#4 2020-02-07 23:07:15       4.28      5.14  -0.863
#5 2020-02-07 23:07:16       4.55      5.11  -0.561
#6 2020-02-07 23:07:17       5.40      5.07   0.323
#...

In rollmean the default alignment is "center" which assigns the mean value to be in between two values. You can change it to either "left" or "right" based on the requirement.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213