我想按日期差異分組。
例如,如果設施 A 中有 7 個病例,但前 5 個病例發生在最后 2 個病例的 14 天之前,我希望它們分在兩個不同的組中(見下面的示例)
| 地點 | 地址 | 開始日期 | 開始日期差異 | 團體 |
|---|---|---|---|---|
| 設施 A | 123 主街 | 2022 年 2 月 7 日 | 0 | 1 |
| 設施 A | 123 主街 | 2022 年 2 月 11 日 | 4 | 1 |
| 設施 A | 123 主街 | 2022 年 2 月 11 日 | 0 | 1 |
| 設施 A | 123 主街 | 2022 年 2 月 11 日 | 0 | 1 |
| 設施 A | 123 主街 | 2022 年 2 月 12 日 | 1 | 1 |
| 設施 A | 123 主街 | 2022 年 3 月 12 日 | 28 | 2 |
| 設施 A | 123 主街 | 2022 年 3 月 17 日 | 5 | 2 |
| 設施 B | 55 福特路 | 2022 年 3 月 16 日 | 0 | 3 |
| 設施 B | 55 福特路 | 2022 年 3 月 16 日 | 0 | 3 |
| 設施 C | 1步大道 | 2022 年 3 月 16 日 | 0 | 4 |
| 設施 C | 1步大道 | 2022 年 3 月 20 日 | 4 | 4 |
| 設施 C | 1步大道 | 2022 年 3 月 22 日 | 2 | 4 |
到目前為止,這是我的代碼:
我被困在如何通過個體觀察之間的日期差異進一步對它們進行分組。
uj5u.com熱心網友回復:
假設我們還沒有diff計算過,并且我們需要轉換start_date成算術上有用的東西。
資料表
library(data.table)
as.data.table(dat)[, start_date := as.Date(start_date, format = "%m/%d/%Y")
][, diff14 := cumsum(c(0, diff(start_date)) > 14), by = location
][, Group2 := rleid(location, diff14)][]
# location address start_date start_date_diff Group diff14 Group2
# <char> <char> <Date> <int> <int> <int> <int>
# 1: Facility A 123 main st 2022-02-07 0 1 0 1
# 2: Facility A 123 main st 2022-02-11 4 1 0 1
# 3: Facility A 123 main st 2022-02-11 0 1 0 1
# 4: Facility A 123 main st 2022-02-11 0 1 0 1
# 5: Facility A 123 main st 2022-02-12 1 1 0 1
# 6: Facility A 123 main st 2022-03-12 28 2 1 2
# 7: Facility A 123 main st 2022-03-17 5 2 1 2
# 8: Facility B 55 ford rd 2022-03-16 0 3 0 3
# 9: Facility B 55 ford rd 2022-03-16 0 3 0 3
# 10: Facility C 1 step ave 2022-03-16 0 4 0 4
# 11: Facility C 1 step ave 2022-03-20 4 4 0 4
# 12: Facility C 1 step ave 2022-03-22 2 4 0 4
dplyr
library(dplyr)
dat %>%
mutate(start_date = as.Date(start_date, format = "%m/%d/%Y")) %>%
group_by(location) %>%
mutate(diff14 = cumsum(c(0, diff(start_date)) > 14)) %>%
group_by(location, diff14) %>%
mutate(Group2 = cur_group_id()) %>%
ungroup()
# # A tibble: 12 x 7
# location address start_date start_date_diff Group diff14 Group2
# <chr> <chr> <date> <int> <int> <int> <int>
# 1 Facility A 123 main st 2022-02-07 0 1 0 1
# 2 Facility A 123 main st 2022-02-11 4 1 0 1
# 3 Facility A 123 main st 2022-02-11 0 1 0 1
# 4 Facility A 123 main st 2022-02-11 0 1 0 1
# 5 Facility A 123 main st 2022-02-12 1 1 0 1
# 6 Facility A 123 main st 2022-03-12 28 2 1 2
# 7 Facility A 123 main st 2022-03-17 5 2 1 2
# 8 Facility B 55 ford rd 2022-03-16 0 3 0 3
# 9 Facility B 55 ford rd 2022-03-16 0 3 0 3
# 10 Facility C 1 step ave 2022-03-16 0 4 0 4
# 11 Facility C 1 step ave 2022-03-20 4 4 0 4
# 12 Facility C 1 step ave 2022-03-22 2 4 0 4
資料
read.md <- structure(list(location = c("Facility A", "Facility A", "Facility A", "Facility A", "Facility A", "Facility A", "Facility A", "Facility B", "Facility B", "Facility C", "Facility C", "Facility C"), address = c("123 main st", "123 main st", "123 main st", "123 main st", "123 main st", "123 main st", "123 main st", "55 ford rd", "55 ford rd", "1 step ave", "1 step ave", "1 step ave"), start_date = c("2/7/2022", "2/11/2022", "2/11/2022", "2/11/2022", "2/12/2022", "3/12/2022", "3/17/2022", "3/16/2022", "3/16/2022", "3/16/2022", "3/20/2022", "3/22/2022"), start_date_diff = c(0L, 4L, 0L, 0L, 1L, 28L, 5L, 0L, 0L, 0L, 4L, 2L), Group = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 4L)), class = "data.frame", row.names = c(NA, -12L))
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/456368.html
