简体   繁体   English

R计数事件发生的总时间

[英]R Count Total Time from occurences of event

Here is my dataframe with dput function . 这是我的带有dput函数的数据dput

structure(list(Time.stamp = structure(1:34, .Label = c("00:07:00", 
"00:12:00", "00:18:00", "00:23:00", "00:28:00", "00:33:00", "00:38:00", 
"00:43:00", "00:48:00", "00:53:00", "00:58:00", "01:03:00", "01:08:00", 
"01:13:00", "01:18:00", "01:23:00", "01:28:00", "01:33:00", "01:38:00", 
"01:43:00", "01:48:00", "01:53:00", "01:58:00", "02:03:00", "02:08:00", 
"02:13:00", "02:18:00", "02:23:00", "02:28:00", "02:33:00", "02:38:00", 
"02:43:00", "02:48:00", "02:53:00"), class = "factor"), Battery.Voltage = c(54.5205, 
54.5205, 54.4447, 54.5205, 43, 44, 45, 46, 54.5205, 54.5205, 
41, 54.5205, 43, 54.5205, 54.5205, 54.5205, 54.5205, 54.5205, 
54.5205, 54.5205, 54.5205, 54.5205, 54.5205, 54.5205, 54.5205, 
54.5205, 54.5205, 54.5205, 45, 54.5205, 54.5205, 54.5205, 54.5205, 
46), Power = c(5997.756589, 6179.146292, 6144.672398, 6071.506469, 
6059.550123, 6021.680184, 6071.501017, 6047.588326, 6005.727486, 
6011.708385, 6019.881107, 6161.209048, 5993.592688, 6011.713837, 
5977.823894, 6053.569224, 6091.433712, 6005.727486, 5991.781142, 
6041.612879, 6001.747489, 6015.693833, 5981.809342, 6065.52557, 
6053.569224, 5997.756589, 5981.814794, 6003.737487, 6061.540122, 
6011.702933, 6013.698383, 6019.684734, 6081.472816, 5969.847545
), f_device_time_date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "01/02/2017", class = "factor"), 
    Condition = structure(c(2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 
    2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L), .Label = c("No", 
    "Yes"), class = "factor")), .Names = c("Time.stamp", "Battery.Voltage", 
"Power", "f_device_time_date", "Condition"), class = "data.frame", row.names = c(NA, 
-34L))

My question is: I want to count the longest repeating No, grouping by dates dates. 我的问题是:我想计算最长的重复否,按日期分组日期。

I want to output the table as 我想将表输出为

Date                 Max_duration of condition No

01-02-2017            9
02-02-2017            5

etc. 等等

My approach: Used rle function to count longest duration of NO and Yes. 我的方法:使用rle函数计算最长持续时间为NO和Yes。 Note: I only need longest No on each day. 注意:每天只需要最长的否。

round_length = rle(data1$battery_test)
data1 %>% group_by(f_device_time_date) %>% tapply(round_length$lengths,round_length$values,max)

output: 
Error in match.fun(FUN) : 
  'round_length$values' is not a function, character or symbol

Any help deeply appreciated. 任何帮助深表感谢。

A solution can be achieved using tidyr:unite function along with dplyr and lubridate packages. 可以使用tidyr:unite函数以及dplyrlubridate软件包来实现解决方案。

The main approach is to find a way to group on continuous appearance of Yes and No in condition column. 主要方法是找到一种对条件列中“ Yes和“ No连续出现进行group的方法。

library(dplyr)
library(tidyr)
library(lubridate)

data1 %>% unite("DateTime", f_device_time_date, Time.stamp, sep = " ") %>%
  mutate(DateTime = dmy_hms(DateTime)) %>%
  mutate(Condition = as.character(Condition)) %>%
  mutate(grp = cumsum(Condition != lag(Condition, default = " "))) %>%
  group_by(Date = date(DateTime), grp, Condition) %>%
  filter(Condition == "No") %>%
  summarise(MaxCountofNo = n()) %>%
  group_by(Date) %>%
  filter(MaxCountofNo == max(MaxCountofNo)) %>%
  select(Date, MaxCountofNo)

# # A tibble: 1 x 2
# # Groups: Date [1]
#         Date       MaxCountofNo
#       <date>            <int>
#   1 2017-02-01            4

The data from OP has got records for only 1 day but my solution will work on data with multiple dates. 来自OP的数据仅获得了1天的记录,但是我的解决方案可以处理具有多个日期的数据。

The above solution calculates maximum number of consecutive No for a day. 上述解决方案计算一天中连续No最大数量。 Perhaps that's what OP intend to have. 也许这就是OP想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM