[英]How to calculate end date R based on next start date, and reshaping the data into date count / time series?
初學者又來了
我一直在尋找關於 stackoverflow 的答案,但沒有成功
如果您知道有在線教程來解釋我應該/可以如何解決這些問題,我很想聽聽。
數據
test <- structure(list(record_id = c(110032, 110032, 110321, 110321,
110032, 110032, 110032, 110032, 110321), start_fu = structure(c(16302,
16302, 17308, 17308, 16302, 16302, 16302, 16302, 17308), class = "Date"),
end_fu = structure(c(17033, 17033, 17828, 17828, 17033, 17033,
17033, 17033, 17828), class = "Date"), start_course = structure(c(16301,
16302, 17307, 17308, 16355, 16325, 16344, 16499, 17824), class = "Date"),
course = structure(c(0, 1, 3, 3, 5, 3, 0, 3, 0), class = c("haven_labelled",
"vctrs_vctr", "double"))), row.names = c(NA, -9L), groups = structure(list(
record_id = c(110032, 110321), .rows = structure(list(c(1L,
2L, 5L, 6L, 7L, 8L), c(3L, 4L, 9L)), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
解釋和變量
所以我從多條記錄中收集了后續數據。 現在,我展示了兩個記錄。 在跟進期間,這些人可以切換課程。 本課程的開始日期已被記錄。
問題 1
我想創建一個名為 stop_course 的變量。 這是根據下一門課程的 start_course 計算的。 (start_course - 1 天) 如果沒有下一門課程,那么應該以 end_fu 日期為准。
預期產出 1
| record_id | start_fu | end_fu | start_course | course | stop_course |
|-----------|------------|------------|--------------|--------|-------------|
| 110032 | 2014-08-20 | 2016-08-20 | 2014-08-19 | 0 | 2014-08-19 |
| 110032 | 2014-08-20 | 2016-08-20 | 2014-08-20 | 1 | 2014-09-11 |
| 110032 | 2014-08-20 | 2016-08-20 | 2014-09-12 | 3 | 2014-09-30 |
| 110032 | 2014-08-20 | 2016-08-20 | 2014-10-01 | 0 | 2014-10-11 |
| 110032 | 2014-08-20 | 2016-08-20 | 2014-10-12 | 5 | 2014-03-04 |
| 110032 | 2014-08-20 | 2016-08-20 | 2015-03-05 | 3 | 2016-08-20 |
| 110321 | 2017-05-22 | 2018-10-24 | 2017-05-21 | 3 | 2017-05-21 |
| 110321 | 2017-05-22 | 2018-10-24 | 2017-05-22 | 3 | 2018-10-19 |
| 110321 | 2017-05-22 | 2018-10-24 | 2018-10-20 | 0 | 2018-10-24 |
問題 2最后,我想為每個 record_id 創建一個包含他們課程的日常列表。 因此:創建一個變量 day_count
預期產出 2
| record_id | day_count | date | course |
|-----------|-----------|------------|--------|
| 110032 | 0 | 2014-08-19 | 0 |
| 110032 | 1 | 2014-08-20 | 1 |
| 110032 | 2 | 2014-08-21 | 1 |
| ... | ... | ... | ... |
| 110032 | 24 | 2014-09-12 | 3 |
| 110032 | 25 | 2013-09-13 | 3 |
| ... | ... | ... | ... |
希望你能幫助我編碼或為我提供一些好的教程
帶寬知識庫
在這里使用dplyr
和tidyr
是一種方法:
我們可以用lead
來獲取下一個日期start_course
並從中減去1天default
值作為last
的值end_fu
每個record_id
。 然后我們可以創建一個從第一個日期到最后一個日期的序列, fill
course
值並創建一個day_count
列。
library(dplyr)
library(tidyr)
test %>%
group_by(record_id) %>%
mutate(stop_course = lead(start_course - 1, default = last(end_fu))) %>%
complete(start_course = seq(min(start_course), max(start_course), 'day')) %>%
select(-ends_with('fu'), -stop_course) %>%
fill(course) %>%
mutate(day_count = row_number() - 1) %>%
rename(date = start_course)
# record_id date course day_count
# <dbl> <date> <dbl+lbl> <dbl>
# 1 110032 2014-08-19 0 0
# 2 110032 2014-08-20 1 1
# 3 110032 2014-08-21 1 2
# 4 110032 2014-08-22 1 3
# 5 110032 2014-08-23 1 4
# 6 110032 2014-08-24 1 5
# 7 110032 2014-08-25 1 6
# 8 110032 2014-08-26 1 7
# 9 110032 2014-08-27 1 8
#10 110032 2014-08-28 1 9
# … with 707 more rows
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.