-1

我正在研究传感器数据库

这是我的数据的一个子集

1

我想为我的所有传感器计算每个“开启”事件的持续时间。知道“ON”的持续时间等于第一个 ON 和第一个 OFF 之间的差例如在传感器“capteur1”的表中,我必须有 41 秒、30 秒、25 秒等。谢谢,

4

1 回答 1

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

希望这可以帮助

塞巴斯蒂安

于 2018-06-15T09:38:43.590 回答