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

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

Calculate the time intervals within each cycle using R

问题

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

我有这样的数据:

data<-data.frame(time=c(20230404001040, 20230404001050,20230404001100, 20230404001110, 20230404001120,20230404001130,
                        20230404001140,20230404001150,20230404001200),
                 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。所以我的期望输出应该如下所示:

data<-data.frame(time=c(20230404001040, 20230404001050,20230404001100, 20230404001110, 20230404001120,20230404001130,
                        20230404001140,20230404001150,20230404001200),
                 on=c("FALSE", "FALSE", "FALSE", "TRUE","TRUE","TRUE","FALSE","FALSE","FALSE"),
                 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:

data&lt;-data.frame(time=c(20230404001040, 20230404001050,20230404001100, 20230404001110, 20230404001120,20230404001130,
                        20230404001140,20230404001150,20230404001200),
                 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:

data&lt;-data.frame(time=c(20230404001040, 20230404001050,20230404001100, 20230404001110, 20230404001120,20230404001130,
                        20230404001140,20230404001150,20230404001200),
                 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_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

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

## 用于为连续状态块添加唯一标签的辅助函数
## 以便稍后进行分组持续时间求和:

get_block_labels <- function(xs){
  rls <- rle(xs)$lengths
  rep(1:length(rls), times = rls)
}

library(dplyr)

data %>%
  arrange(time) %>%
  mutate(time = time |> as.character() |> ymd_hms(),
         dt = (time - lag(time, default = time[1])) |> as.integer(),
         block = get_block_labels(on)
         ) %>%
  group_by(block) %>%
  mutate(dur = cumsum(dt))

输出结果:

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

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

英文:

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

## helper function to uniquely label blocks
## of continuous state for later groupwise
## duration summing:

get_block_labels &lt;- function(xs){
  rls &lt;- rle(xs)$lengths
  rep(1:length(rls), times = rls)
}

library(dplyr)

data |&gt;
  arrange(time) |&gt;
  mutate(time = time |&gt; as.character() |&gt;  ymd_hms(),
         dt = (time - lag(time, default = time[1])) |&gt; as.integer(),
         block = get_block_labels(on)
         ) |&gt;
  group_by(block) |&gt;
  mutate(dur = cumsum(dt))

output:

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

确定