R 如何按组将时间设置为零?
如果我有一个数据帧,它有许多行,如下所示:R 如何按组将时间设置为零?,r,dplyr,aggregate,R,Dplyr,Aggregate,如果我有一个数据帧,它有许多行,如下所示: subject-id activity label timestamp x y z 1 1600 A 2.522077e+14 -0.3647613 8.793503 1.0550842 2 1600 A 2.522077e+14 -0.8797302 9.768784 1.0169983 3 1
subject-id activity label timestamp x y z
1 1600 A 2.522077e+14 -0.3647613 8.793503 1.0550842
2 1600 A 2.522077e+14 -0.8797302 9.768784 1.0169983
3 1600 A 2.522078e+14 2.0014954 11.109070 2.619156
4 1600 A 2.522078e+14 0.4506226 12.651642 0.18455505
5 1600 A 2.522079e+14 -2.1643524 13.928436 -4.4224854
6 1600 A 2.522079e+14 -4.3327790 13.361191 -0.7188721
.
.
.
subject-id activity label timestamp x y z
991 1600 B 2.519876e+14 1.37554930 15.3750460 2.9716187
992 1600 B 2.519877e+14 -3.93443300 17.5387880 2.1100159
993 1600 B 2.519877e+14 -0.08773804 12.7915650 -1.4541016
994 1600 B 2.519878e+14 2.03874200 3.0771484 -1.0537262
995 1600 B 2.519878e+14 -2.55847170 -2.7386780 -2.0985107
996 1600 B 2.519879e+14 -1.35530090 0.3884125 -0.6598511
如何通过组“主题id”将时间设置为零?我想减去该组每个实例的第一次。像这样:
subject-id activity label timestamp x y z
1 1600 A 0 -0.3647613 8.793503 1.0550842
2 1600 A .050354 -0.8797302 9.768784 1.0169983
3 1600 A .100708 2.0014954 11.109070 2.619156
4 1600 A .151062 0.4506226 12.651642 0.18455505
.
.
.
subject-id activity label timestamp x y z
991 1600 B 0 1.37554930 15.3750460 2.9716187
992 1600 B .049355 -3.93443300 17.5387880 2.1100159
993 1600 B .100601 -0.08773804 12.7915650 -1.4541016
注:我为活动B编了一些数字来说明我的意思
我所尝试的:
我尝试创建一个函数并使用聚合应用该函数:
time_zero <- function(vec){
result <- (vec$timestamp - vec$timestamp[1])/10E8
return(result)
}
test <- aggregate(pa, list(pa$`activity label`), FUN = time_zero)
time\u zero我们可以在这里使用ave
:
df$timestamp <- with(df, ave(timestamp, label, FUN = function(x) (x - x[1])/10E8))
或数据表
library(data.table)
setDT(df)[, timestamp := (timestamp - first(timestamp))/10E8, label]
这将用每个标签中的第一个时间戳
减去时间戳
,再除以10E8。非常感谢!我特别喜欢dplyr版本。这对我来说是最有意义的!
library(data.table)
setDT(df)[, timestamp := (timestamp - first(timestamp))/10E8, label]