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

发布于 2025-01-18 07:20:21 字数 782 浏览 2 评论 0原文

在下面的数据集中,我想知道每个 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"

In the data set below, I would like to know how many observations each ID has in 8 day chunks. What would be the best way to approach this?

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)

I have been able to get the total number of observations for each ID, but I'm unsure on how to see how many observations each ID has within each 8 day period in the data set:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗫嚅 2025-01-25 07:20:21

我们可以使用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)

We may create a grouping variable with ceiling_date and then get the count or summarise with n()

library(dplyr)
library(lubridate)
deer %>% 
  group_by(ID) %>%
  mutate(eight_day_period = ceiling_date(date, "8 day")) %>%
  ungroup %>%
  count(ID, eight_day_period)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文