这是我的7ffff7ff3000-7ffff7ff7000 rw-s 00000000 00:16 176796 /dev/shm/shdmem
函数的数据框。
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))
等
我的方法: 使用rle函数计算NO的最长持续时间和是。 注意:我每天只需要最长的No。
Date Max_duration of condition No
01-02-2017 9
02-02-2017 5
任何帮助深表赞赏。
答案 0 :(得分:0)
使用tidyr:unite
函数以及dplyr
和lubridate
包可以实现解决方案。
主要方法是在条件列中找到group
连续出现Yes
和No
的方法。
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
来自OP的数据只有1天的记录,但我的解决方案将处理具有多个日期的数据。
上述解决方案计算一天的最大连续No
次数。或许这就是OP打算拥有的东西。