我有一系列RPM测量,每个测量对应时间。如果我绘制RPM的时间序列,它将如下所示:仅在值为……时记录测量值
指出的问题不是很清楚,但也许这可能会有所帮助,使用基数R:
# first you define your time as date dats$time <-strptime(dats$time, " %d/%m/%Y %I:%M:%S %p") # now a lagged vector, that points out the "lasting" lagged <- c(tail(dats$time , -1),NA) # here added as column dats <- cbind(dats, lagged) dats time rpm lagged 1 2018-07-08 20:08:16 8 2018-07-08 20:08:21 2 2018-07-08 20:08:21 9 2018-07-08 20:08:41 3 2018-07-08 20:08:41 8 2018-07-08 20:09:30 4 2018-07-08 20:09:30 9 2018-07-08 20:19:02 5 2018-07-08 20:19:02 10 2018-07-08 20:23:16 6 2018-07-08 20:23:16 9 2018-07-08 20:23:22 7 2018-07-08 20:23:22 10 2018-07-08 20:23:24 8 2018-07-08 20:23:24 9 2018-07-08 20:23:36 9 2018-07-08 20:23:36 9 2018-07-08 20:23:45 10 2018-07-08 20:23:45 10 <NA> # calculate the full time overall <- max(dats$time)-min(dats$time) overall Time difference of 15.48333 mins # select only the rpm you want, unluckily due your data, I choose 8, because I do not # have 1, you can easily change it dats_rpm <- dats[dats$rpm <= 8,] # let's calculate each duration of the rpm <= 8 dats_rpm$duration <- dats_rpm$lagged - dats_rpm$time # lastly the percentage of the duration of rpm<=8 / overall duration as.double(sum(dats_rpm$duration), units='secs')/as.double(overall, units='secs')*100 [1] 5.812702
希望能帮助到你!
有了数据:
dats <- structure(list(time = c("08/07/2018 08:08:16 PM", "08/07/2018 08:08:21 PM", "08/07/2018 08:08:41 PM", "08/07/2018 08:09:30 PM", "08/07/2018 08:19:02 PM", "08/07/2018 08:23:16 PM", "08/07/2018 08:23:22 PM", "08/07/2018 08:23:24 PM", "08/07/2018 08:23:36 PM", "08/07/2018 08:23:45 PM"), rpm = c(8L, 9L, 8L, 9L, 10L, 9L, 10L, 9L, 9L, 10L)), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"))