R中rows/seconds的偏移平均计算
Offset average calculation by rows/seconds in R
我有以下数据集
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")
我想保留 MinimaxVel
列原样,但计算不同时间段内 GarminVel
的平均值。例如,我有一个 MinimaxVel
值和一个相应的 GarminVel
值。我需要一个新的 GarminVel
值来计算为 mean(GarminVel-1, GarminVel, GarminVel+1)
。即我平均计算的时间段是 3 秒,偏移 1 秒。以下是前 3 秒的预期结果。
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
我想在不同的时间段执行此操作,例如3 - 15 秒。我有谁能帮忙抵消吗?我看过 lag
和 rollmean
但还没有达到我想要的结果
谢谢
你可以在这里使用 rollmean
:
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
#...
在 rollmean
中,默认对齐方式是“居中”,它指定两个值之间的平均值。您可以根据需要将其更改为“左”或“右”。
我有以下数据集
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")
我想保留 MinimaxVel
列原样,但计算不同时间段内 GarminVel
的平均值。例如,我有一个 MinimaxVel
值和一个相应的 GarminVel
值。我需要一个新的 GarminVel
值来计算为 mean(GarminVel-1, GarminVel, GarminVel+1)
。即我平均计算的时间段是 3 秒,偏移 1 秒。以下是前 3 秒的预期结果。
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
我想在不同的时间段执行此操作,例如3 - 15 秒。我有谁能帮忙抵消吗?我看过 lag
和 rollmean
但还没有达到我想要的结果
谢谢
你可以在这里使用 rollmean
:
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
#...
在 rollmean
中,默认对齐方式是“居中”,它指定两个值之间的平均值。您可以根据需要将其更改为“左”或“右”。