如何根据下一个开始日期计算结束日期 R,并将数据重塑为日期计数/时间序列?

How to calculate end date R based on next start date, and reshaping the data into date count / time series?

又是新手

我一直在 Whosebug 上寻找答案,但没有成功

如果您知道有解释我 should/could 如何解决这些问题的在线教程,我很想听听。

数据

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      |
|       ... | ...       | ...        | ...    |

希望你能帮助我编码或提供一些好的教程

体重 KB

这里使用 dplyrtidyr 是一种方法:

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