我正在研究传感器数据库
这是我的数据的子集
我想计算所有传感器的每个'ON'事件的持续时间。 知道'ON'的持续时间等于第一个ON和第一个OFF之间的差值 例如在传感器'capteur1'的表中,我必须有41秒,30秒,25秒等。 谢,
答案 0 :(得分:0)
简短代码 - 尝试一下:
# important - time must be a time-element (strptime or use format for example)
# subset only ON and OFF
ON <- df$time[df$capteur1 %in% "ON"]
OFF <- df$time[df$capteur1 %in% "OFF"]
# Create tempoary element to append to in for-loop
temp <- NULL # temp stands for temporary
name_temp <- NULL
# Loop over ON-Elements
for (i in ON) {
a <- difftime(OFF, i, units = "sec") # difftime of i-element of ON vs all OFF
a <- a[!a < 0] # drop negative seconds as Off was before on
# append positive Seconds - but only the first element - as this was the next OFF
temp <- c(temp, a[1])
name_temp <- c(name_temp, as.character(i))
}
# Give names to elements
names(temp) <- name_temp
# show temp
temp
希望这有帮助
塞巴斯蒂安