[英]Count the number of days from a specific date with ID as a “break variable”
[英]R: Count number of days before specific date by ID
我有多列的數據框,最重要的是 id 和 date。 我想在 R 中創建另一列,它將填充日期間隔日期為 id 的每一行。 像這樣的東西。
id date
1 12/31/2019
1 12/30/2019
2 12/26/2019
2 12/25/2019
2 12/24/2019
3 12/22/2019
3 12/21/2019
3 12/20/2019
3 12/19/2019
4 12/15/2019
4 12/14/2019
4 12/13/2019
做這樣的
id date date count
1 12/31/2019 2
1 12/30/2019 1
2 12/26/2019 3
2 12/25/2019 2
2 12/24/2019 1
3 12/22/2019 4
3 12/21/2019 3
3 12/20/2019 2
3 12/19/2019 1
4 12/15/2019 3
4 12/14/2019 2
4 12/13/2019 1
一種dplyr
可能性可能是:
df %>%
group_by(id) %>%
mutate(date_count = dense_rank(as.Date(date, format = "%m/%d/%Y")))
id date date_count
<int> <chr> <int>
1 1 12/31/2019 2
2 1 12/30/2019 1
3 2 12/26/2019 3
4 2 12/25/2019 2
5 2 12/24/2019 1
6 3 12/22/2019 4
7 3 12/21/2019 3
8 3 12/20/2019 2
9 3 12/19/2019 1
10 4 12/15/2019 3
11 4 12/14/2019 2
12 4 12/13/2019 1
我們可以使用data.table
方法
library(data.table)
setDT(df)[, date_count := frank(as.IDate(date, format = "%m/%d/%Y"),
ties.method = 'dense'), id][]
# id date date_count
# 1: 1 12/31/2019 2
# 2: 1 12/30/2019 1
# 3: 2 12/26/2019 3
# 4: 2 12/25/2019 2
# 5: 2 12/24/2019 1
# 6: 3 12/22/2019 4
# 7: 3 12/21/2019 3
# 8: 3 12/20/2019 2
# 9: 3 12/19/2019 1
#10: 4 12/15/2019 3
#11: 4 12/14/2019 2
#12: 4 12/13/2019 1
df <- structure(list(id = c(1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L,
4L, 4L), date = c("12/31/2019", "12/30/2019", "12/26/2019", "12/25/2019",
"12/24/2019", "12/22/2019", "12/21/2019", "12/20/2019", "12/19/2019",
"12/15/2019", "12/14/2019", "12/13/2019")),
class = "data.frame", row.names = c(NA,
-12L))
另一個data.table
選項:
DT[order(id, as.IDate(date, format="%m/%d/%Y")), dc := rowid(id)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.