dplyr - R calculate session duration from overlapping parts -


i have dataset video streaming playback information. each session split in segments corresponding when paused, moved playhead forward or backward, etc... i'm trying aggregate streaming playback data in such way don't double count overlapping session segments. in simplified example below, each session a, b, c, can have more 1 playback segment , can overlap:

df<-data.frame(session=c(rep("a",3), rep("b",5), "c"),             start=c(1,10,15,1,3,8,14,17,2),             end=c(4,18,20,10,5,12,16,20,10)) 

starting session in 3 segments, can use package intervals calculate non-overlapping session playback time:

library(intervals) x1<-intervals(df[1:3,c('start','end')]) x1 

# object of class intervals

# 3 intervals on r:

# 1 [1, 4]

# 2 [10, 18]

# 3 [15, 20]

interval_intersection(x1) 

# object of class intervals

# 2 intervals on r:

# [1, 4]

# [10, 20]

size(interval_intersection(x1)) 

# [1] 3 10

sum(size(interval_intersection(x1)))  

# [1] 13

so gives me non-overlapping session duration session a. i'd session duration sessions. hoping use dplyr can't seem able integrate intervals:

library(dplyr) df %>%  mutate(interval=intervals(start, end)) %>%  group_by(session) %>% summarise(session_duration=sum(size(interval_intersection(interval))) 

this doesn't work can't use function intervals in mutate.

i tried tapply didn't work either:

df.intervals<-intervals(df[c('start','end')]) tapply(df.intervals, df$session, function(x) sum(size(interval_intersection(x)))) 

any ideas welcome!

you can't begin mutate because result not vector of length nrow(df), "object of class intervals" (even though contains nrow(df) intervals).

so have grouping first:

library(intervals) library(dplyr)  df %>%    group_by(session) %>%   summarise(session_duration = sum(size(interval_intersection(intervals(c(start, end))))))  # # tibble: 3 × 2 #   session session_duration #    <fctr>            <dbl> # 1                     13 # 2       b               16 # 3       c                8 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -