R 如何总结数据点之间的重叠
我有一组动物通过RFID阅读器的数据,看起来像这样-R 如何总结数据点之间的重叠,r,social-networking,lubridate,rfid,R,Social Networking,Lubridate,Rfid,我有一组动物通过RFID阅读器的数据,看起来像这样- 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 我最
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
我最近问了这个问题,(),现在我的数据是这样的-
(将数据组织成10秒的间隔)
我还添加了一个列,总结了时间间隔
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
dat$Interval = interval(dat$start_date_time,dat$end_date_time)
我现在需要找到并总结这些时间间隔的交叉点,并将其作为一个计数,以显示动物相互作用(或同时出现在RFID阅读器上)的次数,如下所示:
(并且不重复反向交互,即A-B,B-A)
谢谢你的帮助 这个问题不太容易回答。也许这是一个很好的起点:
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
库(tidyverse)
图书馆(lubridate)
cbind(
数据[rep(1:nrow(dat[-1,]),nrow(dat[-1,]):1),c(1,4)],
设置名称(
dat[unlist(sapply(2:nrow(dat),seq,to=nrow(dat))),c(1,4)],
c('ID2','Interval2')
)
) %>%
变异(
交互=相交(间隔,间隔2),
间隔=空,
间隔2=空
) %>%
筛选器(!is.na(as.numeric(interactics))%>%
计数(ID,ID2)
##A tible:1 x 3
#ID2N
#
#1 A B 1
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