检查 8 天内每个 ID 的观察次数

Checking the number of observations for each ID within 8 day periods

在下面的数据集中,我想知道每个 ID 在 8 天的数据块中有多少观察值。解决这个问题的最佳方法是什么?

library(lubridate)
library(tidyverse)

date <- rep_len(seq(dmy("01-01-2013"), dmy("31-12-2013"), by = "days"), 300)
ID <-  rep(c("A","B","C"), 50)
deer <- data.frame(date = date,
                   utm_x = runif(length(date), min = 238785, max = 453354.5),
                   utm_y = runif(length(date), min = 4096853.0  , max = 4280487.1 ),
                   ID)

deer$julian <- yday(as.Date(deer$date))
deer$month <- month(deer$date)
deer$year <- year(deer$date)

我已经能够获得每个 ID 的观察总数,但我不确定如何查看每个 ID 在数据集中每 8 天内有多少观察:

# Observation Distribution 
count <- data.frame(table(df$AnimalID))
colnames(count)[1] <- "ID"

我们可以用ceiling_date创建一个分组变量,然后用n()

得到countsummarise
library(dplyr)
library(lubridate)
deer %>% 
  group_by(ID) %>%
  mutate(eight_day_period = ceiling_date(date, "8 day")) %>%
  ungroup %>%
  count(ID, eight_day_period)