У меня есть df, подобный этому
ID <- c("A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B")
MEASUREMENT <- c("Length","Length","Length","Length","Width","Width","Width","Width","Length","Length","Length","Length","Width","Width","Width","Width")
Step <- c("1","2","3","4","1","2","3","4","1","2","3","4","1","2","3","4")
StartDatetime <- c("2015-09-29 00:00:13","2015-09-29 00:02:13","2015-09-29 00:04:13","2015-09-29 00:06:16","2015-09-29 00:07:12","2015-09-29 00:10:13","2015-09-29 00:10:23","2015-09-29 00:12:15","2015-09-29 00:14:13","2015-09-29 00:16:13","2015-09-29 00:19:14","2015-09-29 00:20:13","2015-09-29 00:24:13","2015-09-29 00:26:19","2015-09-29 00:27:20","2015-09-29 00:29:13")
EndDatetime <- c("2015-09-29 00:01:13","2015-09-29 00:03:13","2015-09-29 00:05:23","2015-09-29 00:07:11","2015-09-29 00:08:12","2015-09-29 00:10:23","2015-09-29 00:11:13","2015-09-29 00:13:13","2015-09-29 00:15:13","2015-09-29 00:17:19","2015-09-29 00:20:13","2015-09-29 00:22:13","2015-09-29 00:26:13","2015-09-29 00:27:13","2015-09-29 00:28:13","2015-09-29 00:32:13")
df1 <- data.frame(ID,MEASUREMENT,Step,StartDatetime,EndDatetime)
df1$StartDatetime <- as.POSIXct(df1$StartDatetime)
df1$EndDatetime <- as.POSIXct(df1$EndDatetime)
Я пытаюсь вычислить истекшее время (с) между шагами, сгруппированными по ID и измерению
If Step = 1 then the elapsed time = 0
If Step = 2 then elapsed time = StartDatetime (Step 2) - EndDatetime (Step 1)
Приведенная выше логика применяется дляШаг = 3 и 4.
Мой желаемый вывод - это
ID MEASUREMENT Step StartDatetime EndDatetime ElapsedTime
A Length 1 2015-09-29 00:00:13 2015-09-29 00:01:13 0
A Length 2 2015-09-29 00:02:13 2015-09-29 00:03:13 60
A Length 3 2015-09-29 00:04:13 2015-09-29 00:05:23 60
A Length 4 2015-09-29 00:06:16 2015-09-29 00:07:11 53
A Width 1 2015-09-29 00:07:12 2015-09-29 00:08:12 0
A Width 2 2015-09-29 00:10:13 2015-09-29 00:10:23 121
A Width 3 2015-09-29 00:10:23 2015-09-29 00:11:13 0
A Width 4 2015-09-29 00:12:15 2015-09-29 00:13:13 62
B Length 1 2015-09-29 00:14:13 2015-09-29 00:15:13 0
B Length 2 2015-09-29 00:16:13 2015-09-29 00:17:19 60
B Length 3 2015-09-29 00:19:14 2015-09-29 00:20:13 115
B Length 4 2015-09-29 00:20:13 2015-09-29 00:22:13 0
B Width 1 2015-09-29 00:24:13 2015-09-29 00:26:13 0
B Width 2 2015-09-29 00:26:19 2015-09-29 00:27:13 6
B Width 3 2015-09-29 00:27:20 2015-09-29 00:28:13 7
B Width 4 2015-09-29 00:29:13 2015-09-29 00:32:13 60
Я пытаюсь так, но не понимаю
library(plyr)
df <- ddply(df1, .(ID,MEASUREMENT,Step),
summarize,
ElapsedTime=as.numeric(difftime(df1$StartDatetime, df1$EndDatetime,
units = "secs")))
Как мне выполнить мойжелаемый вывод?Может ли кто-нибудь указать мне правильное направление?