时间序列的时间百分比

时间:2019-02-22 02:52:35

标签: r matlab time timestamp time-series

我有一系列RPM测量,并且每个测量都对应于时间。如果绘制RPM的时间序列,它将看起来像这样: enter image description here

仅在更改值时才记录测量值,例如,如果21:01为0 RPM,而21:02也是0 RPM,则不会记录21:02上的数据。

我的目标是弄清楚如何确定在此期间(从8PM到5AM)RPM低于1的时间百分比?

样本数据:

# A tibble: 97 x 2
   time                       rpm
   <chr>                    <dbl>
 1 '08/07/2018 08:08:16 PM'    8.
 2 '08/07/2018 08:08:21 PM'    9.
 3 '08/07/2018 08:08:41 PM'    8.
 4 '08/07/2018 08:09:30 PM'    9.
 5 '08/07/2018 08:19:02 PM'   10.
 6 '08/07/2018 08:23:16 PM'    9.
 7 '08/07/2018 08:23:22 PM'   10.
 8 '08/07/2018 08:23:24 PM'    9.
 9 '08/07/2018 08:23:36 PM'    9.
10 '08/07/2018 08:23:45 PM'   10.
# ... with 87 more rows

1 个答案:

答案 0 :(得分:0)

所指出的问题不是很清楚,但是也许可以使用基数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"))