我想计算在日期向量定义的每个时期内发生的所有事件。向量表示每个时期的第一天。结果应该是输入向量长度和出现次数相同的向量。
我提出了一个非常低效的“循环”解决方案(见下文)。我想知道是否有办法更快地处理同一任务。
events <- c("2000-01-05", "2000-02-08", "2000-04-09", "2000-02-08", "2000-03-13", "2000-03-13")
# Create vector of dates (in this case 52, 7 days periods)
week_vector = as.Date("2000-01-01")
i <- 1; N <- 51
while (i <= N) {
week_vector = append(week_vector, as.Date(week_vector[i] + 7))
i <- i + 1
}
i <- 1; N <- length(week_vector)
while (i <= N) {
occurrences_by_week <- sum(events >= week_vector[i] & events < week_vector[i] + 7)
}
我最初提出了这个解决方案(使用动物园包的rollapply
)。但是对于rollapply
,我无法定义我希望开始对事件进行分组的那一天:
frequency <- as.data.frame(table(as.Date(events)))
frequency.zoo <- read.zoo(frequency)
frequency.zoo.week <- rollapply(frequency.zoo, 7, sum, by = 7)
答案 0 :(得分:3)
这样的东西?
events <- as.Date(c("2000-01-05", "2000-02-08", "2000-04-09", "2000-02-08",
"2000-03-13", "2000-03-13"))
week_vector <- seq(from = as.Date("2000-01-01"), to = as.Date("2000-12-23"), by = 7)
# or arguments more similar to the wording in the question, "52 [dates], 7 days periods":
week_vector <- seq(from = as.Date("2000-01-01"), length.out = 52, by = 7)
events2 <- cut(events, breaks = week_vector)
table(events2)
# 2000-01-01 2000-01-08 2000-01-15 2000-01-22 2000-01-29 2000-02-05 2000-02-12 2000-02-19
# 1 0 0 0 0 2 0 0
# 2000-02-26 2000-03-04 2000-03-11 2000-03-18 2000-03-25 2000-04-01 2000-04-08 2000-04-15
# 0 0 2 0 0 0 1 0
# 2000-04-22 2000-04-29 2000-05-06 2000-05-13 2000-05-20 2000-05-27 2000-06-03 2000-06-10
# 0 0 0 0 0 0 0 0
# 2000-06-17 2000-06-24 2000-07-01 2000-07-08 2000-07-15 2000-07-22 2000-07-29 2000-08-05
# 0 0 0 0 0 0 0 0
# 2000-08-12 2000-08-19 2000-08-26 2000-09-02 2000-09-09 2000-09-16 2000-09-23 2000-09-30
# 0 0 0 0 0 0 0 0
# 2000-10-07 2000-10-14 2000-10-21 2000-10-28 2000-11-04 2000-11-11 2000-11-18 2000-11-25
# 0 0 0 0 0 0 0 0
# 2000-12-02 2000-12-09 2000-12-16
# 0 0 0
答案 1 :(得分:1)
使用cut
和table
:
events <- c("2000-01-05", "2000-02-08", "2000-04-09", "2000-02-08", "2000-03-13", "2000-03-13")
events <- as.Date(events)
events_week <- cut(events, breaks = "week")
table(events_week)
使用自定义中断:
breaks_custom = c("2000-01-01", "2000-02-01", "2000-03-01", "2000-05-01")
breaks_custom = as.Date(breaks_custom)
events_cut <- cut(events, breaks = breaks_custom)
table(events_cut)