【问题标题】:Percentage of time of a time series [closed]时间序列的时间百分比[关闭]
【发布时间】:2019-07-16 02:15:17
【问题描述】:

我有一系列 RPM 测量值,每个测量值对应于时间。如果我绘制 RPM 的时间序列,它将如下所示:

仅在值发生变化时记录测量值,例如如果 21:01 为 0 RPM,并且 21:02 也是 0 RPM,则不会记录 21:02 的数据。

我的目标是弄清楚如何确定这段时间(晚上 8 点到凌晨 5 点)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

【问题讨论】:

  • 您能否提供一个数据示例,最好使用dput(your_data)?我可能会通过添加一个变量来表示直到下一次测量(或周期结束)的时间,然后将 RPM
  • 我只是编辑它。
  • 正如@JonSpring 所说,请添加dput(&lt;your_data&gt;)dput(head(&lt;your_data&gt;))。否则我们无法复制。
  • 但是问题到底出在哪里?这是一个数学问题,您想知道必须使用什么等式,还是从字符串中提取时间戳或其他什么问题?

标签: r matlab time timestamp time-series


【解决方案1】:

所指出的问题不是很清楚,但这可能会有所帮助,使用基础 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"))

【讨论】: