使用R计算每个周期内的时间间隔。

huangapple go评论98阅读模式
英文:

Calculate the time intervals within each cycle using R

问题

I have translated the content you provided into Chinese as you requested:

我有这样的数据:

  1. data<-data.frame(time=c(20230404001040, 20230404001050,20230404001100, 20230404001110, 20230404001120,20230404001130,
  2. 20230404001140,20230404001150,20230404001200),
  3. on=c("FALSE", "FALSE", "FALSE", "TRUE","TRUE","TRUE","FALSE","FALSE","FALSE"))

'time' 以ymd_hms格式表示。我认为可以使用 data[,1] <- ymd_hms(data[,1])

如果 on 为 FALSE,表示开关关闭。
如果 on 为 TRUE,表示开关打开。

我想要计算每次开关事件的持续时间。每行的 time 间隔为10秒。因此,我可以计算每个开关事件内有多少行,然后乘以10。所以我的期望输出应该如下所示:

  1. data<-data.frame(time=c(20230404001040, 20230404001050,20230404001100, 20230404001110, 20230404001120,20230404001130,
  2. 20230404001140,20230404001150,20230404001200),
  3. on=c("FALSE", "FALSE", "FALSE", "TRUE","TRUE","TRUE","FALSE","FALSE","FALSE"),
  4. time_after_switch=c(0,10,20,0,10,20,0,10,20))

对于我的 data,前3行是关闭事件,接下来的3行是打开事件,最后的3行是关闭事件。所以我可以将它视为3个循环。在每个循环内,持续时间分别为0,10,20,0,10,20,0,10,20。我想要编写R代码来计算 time_after_switch 的值。

英文:

I have a data like this:

  1. data&lt;-data.frame(time=c(20230404001040, 20230404001050,20230404001100, 20230404001110, 20230404001120,20230404001130,
  2. 20230404001140,20230404001150,20230404001200),
  3. on=c(&quot;FALSE&quot;, &quot;FALSE&quot;, &quot;FALSE&quot;, &quot;TRUE&quot;,&quot;TRUE&quot;,&quot;TRUE&quot;,&quot;FALSE&quot;,&quot;FALSE&quot;,&quot;FALSE&quot;))

'time' is written as ymd_hms representation. I think I can use data[,1] &lt;- ymd_hms(data[,1]).
If on is FALSE, it means that the switch is off.
If on is TRUE, it means that the switch is on.

I want to calculate the duration time of each on/off event. Each row of time is 10-second interval. So I can count how many rows within each on/off event and multiply to 10. So my desired output should look like this:

  1. data&lt;-data.frame(time=c(20230404001040, 20230404001050,20230404001100, 20230404001110, 20230404001120,20230404001130,
  2. 20230404001140,20230404001150,20230404001200),
  3. on=c(&quot;FALSE&quot;, &quot;FALSE&quot;, &quot;FALSE&quot;, &quot;TRUE&quot;,&quot;TRUE&quot;,&quot;TRUE&quot;,&quot;FALSE&quot;,&quot;FALSE&quot;,&quot;FALSE&quot;),
  4. time_after_switch=c(0,10,20,0,10,20,0,10,20))

For my data first 3 rows are switch off event, next 3 rows are switch on event, finally last 3 rows are switch off event. So I can think of it as 3 cycles. Within each cycle, the duration times are 0,10,20,0,10,20,0,10,20. I want to make r code calculating the values of time_after_switch.

答案1

得分: 1

以下是代码部分的中文翻译:

  1. ## 用于为连续状态块添加唯一标签的辅助函数
  2. ## 以便稍后进行分组持续时间求和:
  3. get_block_labels <- function(xs){
  4. rls <- rle(xs)$lengths
  5. rep(1:length(rls), times = rls)
  6. }
  7. library(dplyr)
  8. data %>%
  9. arrange(time) %>%
  10. mutate(time = time |> as.character() |> ymd_hms(),
  11. dt = (time - lag(time, default = time[1])) |> as.integer(),
  12. block = get_block_labels(on)
  13. ) %>%
  14. group_by(block) %>%
  15. mutate(dur = cumsum(dt))

输出结果:

  1. + # 一个 tibble: 9 x 5
  2. # 组: block [3]
  3. time on dt block dur
  4. <dttm> <chr> <int> <int> <int>
  5. 1 2023-04-04 00:10:40 FALSE 0 1 0
  6. 2 2023-04-04 00:10:50 FALSE 10 1 10
  7. 3 2023-04-04 00:11:00 FALSE 10 1 20
  8. 4 2023-04-04 00:11:10 TRUE 10 2 10
  9. 5 2023-04-04 00:11:20 TRUE 10 2 20
  10. 6 2023-04-04 00:11:30 TRUE 10 2 30
  11. 7 2023-04-04 00:11:40 FALSE 10 3 10
  12. 8 2023-04-04 00:11:50 FALSE 10 3 20
  13. 9 2023-04-04 00:12:00 FALSE 10 3 30

请注意,代码部分已经翻译,输出结果保持原文。如果您需要更多帮助,请告诉我。

英文:

one approach (using the actual time spans between log entries):

  1. ## helper function to uniquely label blocks
  2. ## of continuous state for later groupwise
  3. ## duration summing:
  4. get_block_labels &lt;- function(xs){
  5. rls &lt;- rle(xs)$lengths
  6. rep(1:length(rls), times = rls)
  7. }
  8. library(dplyr)
  9. data |&gt;
  10. arrange(time) |&gt;
  11. mutate(time = time |&gt; as.character() |&gt; ymd_hms(),
  12. dt = (time - lag(time, default = time[1])) |&gt; as.integer(),
  13. block = get_block_labels(on)
  14. ) |&gt;
  15. group_by(block) |&gt;
  16. mutate(dur = cumsum(dt))

output:

  1. + # A tibble: 9 x 5
  2. # Groups: block [3]
  3. time on dt block dur
  4. &lt;dttm&gt; &lt;chr&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;
  5. 1 2023-04-04 00:10:40 FALSE 0 1 0
  6. 2 2023-04-04 00:10:50 FALSE 10 1 10
  7. 3 2023-04-04 00:11:00 FALSE 10 1 20
  8. 4 2023-04-04 00:11:10 TRUE 10 2 10
  9. 5 2023-04-04 00:11:20 TRUE 10 2 20
  10. 6 2023-04-04 00:11:30 TRUE 10 2 30
  11. 7 2023-04-04 00:11:40 FALSE 10 3 10
  12. 8 2023-04-04 00:11:50 FALSE 10 3 20
  13. 9 2023-04-04 00:12:00 FALSE 10 3 30

huangapple
  • 本文由 发表于 2023年5月6日 22:59:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定