Home > OS >  R Creating a New Column with Max/Min Dates
R Creating a New Column with Max/Min Dates

Time:10-12

I have a dataframe, and I am trying to create two new columns, max_start_date and max_end_date, based on a combination of a person id and a date_diff column. My problem is, a person might have multiple date differences. My example is below:

ex <- structure(list(person_id = c(1, 1, 1, 1, 1, 1, 1, 1, 1), serv_from_dt = structure(c(18262, 
                                                                                           18262, 18263, 18264, 18275, 18275, 18275, 18278, 18291), class = "Date"), 
                      serv_to_dt = structure(c(18262, 18265, 18263, 18264, 18275, 
                                               18278, 18278, 18278, 18291), class = "Date"), days_diff = c(0, 
                                                                                                           3, 0, 0, 0, 3, 3, 0, 0)), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                   -9L), class = c("data.table", "data.frame"))

                                                                                                                                                                                                               

As you can see, the groups of min/max dates are: 2020-01-01/2020-01-04 (days_diff of 3), 2020-01-14/2020-01-17 (days_diff of 3), and 2020-01-30/2020-01-30 (since there are no days overlapping with 2020-01-30).

My desired output looks like so:

output <- structure(list(person_id = c(1, 1, 1, 1, 1, 1, 1, 1, 1), serv_from_dt = structure(c(18262, 
                                                                                           18262, 18263, 18264, 18275, 18275, 18275, 18278, 18291), class = "Date"), 
                      serv_to_dt = structure(c(18262, 18265, 18263, 18264, 18275, 
                                               18278, 18278, 18278, 18291), class = "Date"), days_diff = c(0, 
                                                                                                           3, 0, 0, 0, 3, 3, 0, 0), max_start_date = c("2020-01-01", 
                                                                                                                                                       "2020-01-01", "2020-01-01", "2020-01-01", "2020-01-14", "2020-01-14", 
                                                                                                                                                       "2020-01-14", "2020-01-14", "2020-01-30"), max_end_date = c("2020-01-04", 
                                                                                                                                                                                                                   "2020-01-04", "2020-01-04", "2020-01-04", "2020-01-17", "2020-01-17", 
                                                                                                                                                                                                                   "2020-01-17", "2020-01-17", "2020-01-30")), row.names = c(NA, 
                                                                                                                                                                                                                                                                             -9L), class = c("data.table", "data.frame"))

As of right now, I have

claims_sample[,days_diff := time_length(serv_to_dt-serv_from_dt, unit = 'days'), prs_nat_key][,`:=`(max_start_date =
                                                                                                      serv_from_dt[which.max(days_diff)],
                                                                                                    max_end_date = serv_to_dt[which.max(days_diff)]), prs_nat_key]

But this only repeats 2020-01-01 and 2020-01-04 throughout the entire columns. I would really appreciate any help and suggestions on how to solve this. Thanks in advance!

CodePudding user response:

Here is a suggestion: The assumption is that each group of date has 4 dates!

library(dplyr)
ex %>% 
  group_by(person_id, x = ceiling(row_number()/4)) %>% 
  mutate(max_start_date = min(serv_from_dt),
         max_end_date = max(serv_to_dt)
         )
  person_id serv_from_dt serv_to_dt days_diff     x max_start_date max_end_date
      <dbl> <date>       <date>         <dbl> <dbl> <date>         <date>      
1         1 2020-01-01   2020-01-01         0     1 2020-01-01     2020-01-04  
2         1 2020-01-01   2020-01-04         3     1 2020-01-01     2020-01-04  
3         1 2020-01-02   2020-01-02         0     1 2020-01-01     2020-01-04  
4         1 2020-01-03   2020-01-03         0     1 2020-01-01     2020-01-04  
5         1 2020-01-14   2020-01-14         0     2 2020-01-14     2020-01-17  
6         1 2020-01-14   2020-01-17         3     2 2020-01-14     2020-01-17  
7         1 2020-01-14   2020-01-17         3     2 2020-01-14     2020-01-17  
8         1 2020-01-17   2020-01-17         0     2 2020-01-14     2020-01-17  
9         1 2020-01-30   2020-01-30         0     3 2020-01-30     2020-01-30  
  • Related