Search code examples
rdatetimedplyrlubridate

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


Beginner here again

I have been looking for an answer on stackoverflow, without succes

If you know have online tutorials which explains how I should/could tackle these problems, I would love to hear.

DATA

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"))

EXPLANATION AND VARIABLES

So I collected follow-up data from multiple records. Now, I am showing two records. During the follow-up, these people can switch courses. The start date of this course has been recorded.

  • record_id = individual unique id
  • start_fu = start of follow-up
  • end_fu = end of follow-up
  • start_course = start date of the course
  • course = which course was started

QUESTION 1

I want to create a variable called stop_course. This is calculated based on the start_course of the next course. (start_course - 1 day) If there is no next course, then it should be based on the end_fu date.

EXPECTED OUTPUT 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  |

QUESTION 2 At the end I want to create per record_id a day to day list with their courses. Thus: create a variable day_count

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

Hope you can help me with coding or providing me some good tutorials

BW KB


Solution

  • Using dplyr and tidyr here is a way :

    We can use lead to get next date of start_course and subtract 1 day from it with default value as last value from end_fu in each record_id. We can then create a sequence from first date till last date, fill the course value and create a day_count column.

    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