r - 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
默认对齐方式是“中心”,它将平均值分配在两个值之间。您可以根据需要将其更改为“左”或“右”。
推荐阅读
- css - 为什么我的图标显示图像重复?
- java - 如何判断一个对象是可序列化的还是不可序列化的?
- javascript - 用 SVG 文本做 polyfill 线条效果的最佳方法是什么?
- javascript - 如何在网站中显示来自 Firestore 下载 URL 的图像
- reactjs - React Smooth DND Library 导致和“非法调用错误”
- c++ - 每毫秒写入 fstream 与批量写入
- python - DRF 错误:无法使用嵌套序列化程序上的视图名称解析超链接关系的 URL
- r - 为什么 plm::pvcm 的汇总函数会给出模型 = "within" 的系数分位数?
- android - JobScheduler 有时会在应用空闲时停止工作
- sql - 如果第一个 SQL 语句返回 NULL(或什么都不返回),则运行第二个 Select 语句