问题
I have a data set of animals passing an RFID reader, it looks like this -
ID date_time
A 2019-11-02 08:07:47
B 2019-11-02 08:07:48
A 2019-11-02 08:07:49
A 2019-11-02 08:07:50
A 2019-11-02 08:09:12
A 2019-11-02 08:09:13
B 2019-11-02 08:09:17
I asked this question recently, (combine multiple rows into one time interval), and now my data looks like this - (with the data organised into ten second intervals)
ID start_date_time. end_date_time
A 2019-11-02 08:07:47 2019-11-02 08:07:50
B 2019-11-02 08:07:48 2019-11-02 08:07:48
A 2019-11-02 08:09:12 2019-11-02 08:09:13
B 2019-11-02 08:09:17 2019-11-02 08:09:47
I have also added a column which summarises the intervals
dat$Interval = interval(dat$start_date_time,dat$end_date_time)
I now need to find and summarise where these intervals intersect and produce this as a count, to show the number of times animals interact (or are present at the RFID reader at the same time) something like this - (and without repeating reverse interactions, i.e. A-B, B-A)
ID ID2 Interactions(n)
A A 0
A B 1
A C 3
Any help appreciated.
回答1:
Not really easy to answer, this question. Maybe this might be a good starting point:
library(tidyverse)
library(lubridate)
cbind(
dat[rep(1:nrow(dat[-1, ]), nrow(dat[-1, ]):1), c(1, 4)],
setNames(
dat[unlist(sapply(2:nrow(dat), seq, to = nrow(dat))), c(1, 4)],
c('ID2', 'Interval2')
)
) %>%
mutate(
interacts = intersect(Interval, Interval2),
Interval = NULL,
Interval2 = NULL
) %>%
filter(!is.na(as.numeric(interacts))) %>%
count(ID, ID2)
# # A tibble: 1 x 3
# ID ID2 n
# <chr> <chr> <int>
# 1 A B 1
来源:https://stackoverflow.com/questions/58957189/how-to-summarise-overlaps-between-data-points