简体   繁体   中英

Group, then create a 'break' if the datetime exceeds a certain time, creating a new value within original grouped column (R, dplyr)

I have dataset, df,

  Subject      Folder     Message    Date
  A            Out                   9/9/2019 5:46:38 PM
  A            Out                   9/9/2019 5:46:40 PM
  A            Out                   9/9/2019 5:46:42 PM
  A            Out                   9/9/2019 5:46:43 PM
  A            Out                   9/9/2019 9:30:00 PM
  A            Out                   9/9/2019 9:30:01 PM
  B            Out                   9/9/2019 9:35:00 PM
  B            Out                   9/9/2019 9:35:01 PM

I am trying to group this by Subject, find the duration, and create a new Duration column. I also wish to create a threshold if the Date time exceeds a certain amount of time. My dilemma is that within Group A, the time goes from 5:46 in the 4th row to 9:30 in the 5th row. This gives an inaccurate duration in Group A. I wish to 'break' that time and find the new time duration while creating a new value (A1) in the Subject when the time exceeds 10 minutes. I am not sure if I should use a loop for this?

 Subject   Duration   Group
 A         5 sec      outdata1
 A1        1 sec      outdata2
 B         1 sec      outdata3

Here is my dput:

structure(list(Subject = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L), .Label = c("A", "B"), class = "factor"), Folder = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Out", class = "factor"), 
Message = c("", "", "", "", "", "", "", ""), Date = structure(1:8, .Label = c("9/9/2019 5:46:38 PM", 
"9/9/2019 5:46:40 PM", "9/9/2019 5:46:42 PM", "9/9/2019 5:46:43 PM", 
"9/9/2019 9:30:00 PM", "9/9/2019 9:30:01 PM", "9/9/2019 9:35:00 PM", 
"9/9/2019 9:35:01 PM"), class = "factor")), row.names = c(NA, 
-8L), class = "data.frame")

This is what I tried:

thresh <- duration(10, units = "minutes")

df %>%  
mutate(Date = mdy_hms(Date)) %>% 
transmute(Subject, Duration = diff = difftime(as.POSIXct(Date, format = 
"%m/%d/%Y %I:%M:%S %p"),as.POSIXct(Date, 
format = "%m/%d/%Y %I:%M:%S %p" ), units = "secs")) %>% 
ungroup %>% 
distinct %>% 
mutate(grp = str_c("Outdata", row_number()))

 mutate(delta = if_else(grp < thresh1, grp, NA_real_))

We can calculate the duration between consecutive Date values to create new group and then calculate the difference in time between min and max in each group.

library(dplyr)
thresh <- 10

df %>%  
  mutate(Date = as.POSIXct(Date, format = "%m/%d/%Y %I:%M:%S %p")) %>%
  group_by(Subject, Group = cumsum(difftime(Date, 
            lag(Date, default = first(Date)), units = "mins") > thresh)) %>%
  summarise(Duration = difftime(max(Date), min(Date), units = "secs")) %>%
  ungroup %>%
  mutate(Group = paste0('outdata', row_number()))

# A tibble: 3 x 3
#  Subject Group    Duration
#  <fct>   <chr>    <drtn>  
#1 A       outdata1 5 secs  
#2 A       outdata2 1 secs  
#3 B       outdata3 1 secs  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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