rjen
rjen

Reputation: 1972

Group-wise selective sum of dates outside intervals

This question is a variation of the question asked here.

I have the following kind of data:

library(tidyverse)
library(lubridate)

data <- tibble(a = c(1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3),
               b = c('x', 'y', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z'),
               c = c('ps', 'ps', 'qs', 'rs', 'rs', 'rs', 'rs', 'rs', 'rs', 'rs', 'rs'),
               d = c(100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100),
               strt = ymd(c('2019-03-20', '2020-01-01', '2018-01-02', '2020-05-01', '2016-01-01', '2020-03-01', '2020-01-01', '2020-01-01', '2020-01-02', '2020-01-01', '2019-10-01')),
               fnsh = ymd(c('3019-03-20', '3020-01-01', '3018-01-02', '2020-06-01', '2016-05-01', '2020-04-01', '2020-06-10', '2020-06-10', '2020-06-10', '2020-06-18', '2019-11-01')))

I am doing a group-wise operation based on the variables a, b and c (i.e. data %>% group_by(a, b, c)). For each group, the rows with genuine starting dates within the last year are of interest. A strt is genuine if it is not bigger than the strt and smaller than or equal to the fnsh of any other row in the group. A strt can thus be genuine even though there is another strt in the group with the same value.

The challenge is to make a selective sum of the genuine strts within each group. In making that sum, a collection of identical genuine strts within a group should count as one.

The following identifies the genuine starting dates, but it does not provide the sum:

library(tidyverse)

data %>%
  group_by(a, b, c) %>%
  mutate(begin = +(map_lgl(strt, ~ sum(strt < .x & .x <= fnsh) == 0) &
                   strt > today(tzone = 'CET') - years(1) &
                   strt <= today(tzone = 'CET')))

The above returns:

       a b     c         d strt       fnsh       begin
   <dbl> <chr> <chr> <dbl> <date>     <date>     <int>
 1     1 x     ps      100 2019-03-20 3019-03-20     0
 2     1 y     ps      200 2020-01-01 3020-01-01     1
 3     2 z     qs      300 2018-01-02 3018-01-02     0
 4     3 z     rs      400 2020-05-01 2020-06-01     0
 5     3 z     rs      500 2016-01-01 2016-05-01     0
 6     3 z     rs      600 2020-03-01 2020-04-01     0
 7     3 z     rs      700 2020-01-01 2020-06-10     1
 8     3 z     rs      800 2020-01-01 2020-06-10     1
 9     3 z     rs      900 2020-01-02 2020-06-10     0
10     3 z     rs     1000 2020-01-01 2020-06-18     1
11     3 z     rs     1100 2019-10-01 2019-11-01     1

What is needed is something like:

       a b     c         d strt       fnsh       groupBeginSum
   <dbl> <chr> <chr> <dbl> <date>     <date>             <int>
 1     1 x     ps      100 2019-03-20 3019-03-20             0
 2     1 y     ps      200 2020-01-01 3020-01-01             1
 3     2 z     qs      300 2018-01-02 3018-01-02             0
 4     3 z     rs      400 2020-05-01 2020-06-01             2
 5     3 z     rs      500 2016-01-01 2016-05-01             2
 6     3 z     rs      600 2020-03-01 2020-04-01             2
 7     3 z     rs      700 2020-01-01 2020-06-10             2
 8     3 z     rs      800 2020-01-01 2020-06-10             2
 9     3 z     rs      900 2020-01-02 2020-06-10             2
10     3 z     rs     1000 2020-01-01 2020-06-18             2
11     3 z     rs     1100 2019-10-01 2019-11-01             2

How to make a sum for each group that counts a collection of identical genuine strts as one?

Upvotes: 0

Views: 62

Answers (1)

GGamba
GGamba

Reputation: 13680

The task is to count the number of unique genuine dates. We can use n_distinct on the filtered vector of strt: n_distinct(strt[genuine])

Note that I ditched the type casting of the genuine columns (called begin in your data) as I would have to re-cast to logical afterward.

Hope this helps:

library(tidyverse)
library(lubridate)

df %>%
  group_by(a, b, c) %>%
  mutate(genuine = map_lgl(strt, ~ sum(strt < .x & .x <= fnsh) == 0) &
                     strt > today(tzone = 'CET') - years(1) &
                     strt <= today(tzone = 'CET'),
         groupBeginSum = n_distinct(strt[genuine]))
#> # A tibble: 11 x 8
#> # Groups:   a, b, c [4]
#>        a b     c         d strt       fnsh       genuine groupBeginSum
#>    <dbl> <chr> <chr> <dbl> <date>     <date>     <lgl>         <int>
#>  1     1 x     ps      100 2019-03-20 3019-03-20 FALSE             0
#>  2     1 y     ps      200 2020-01-01 3020-01-01 TRUE              1
#>  3     2 z     qs      300 2018-01-02 3018-01-02 FALSE             0
#>  4     3 z     rs      400 2020-05-01 2020-06-01 FALSE             2
#>  5     3 z     rs      500 2016-01-01 2016-05-01 FALSE             2
#>  6     3 z     rs      600 2020-03-01 2020-04-01 FALSE             2
#>  7     3 z     rs      700 2020-01-01 2020-06-10 TRUE              2
#>  8     3 z     rs      800 2020-01-01 2020-06-10 TRUE              2
#>  9     3 z     rs      900 2020-01-02 2020-06-10 FALSE             2
#> 10     3 z     rs     1000 2020-01-01 2020-06-18 TRUE              2
#> 11     3 z     rs     1100 2019-10-01 2019-11-01 TRUE              2

Created on 2020-06-18 by the reprex package (v0.3.0)

Data:

df <- tibble(a = c(1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3),
             b = c('x', 'y', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z'),
             c = c('ps', 'ps', 'qs', 'rs', 'rs', 'rs', 'rs', 'rs', 'rs', 'rs', 'rs'),
             d = c(100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100),
             strt = ymd(c('2019-03-20', '2020-01-01', '2018-01-02', '2020-05-01', '2016-01-01', '2020-03-01', '2020-01-01', '2020-01-01', '2020-01-02', '2020-01-01', '2019-10-01')),
             fnsh = ymd(c('3019-03-20', '3020-01-01', '3018-01-02', '2020-06-01', '2016-05-01', '2020-04-01', '2020-06-10', '2020-06-10', '2020-06-10', '2020-06-18', '2019-11-01')))

Upvotes: 1

Related Questions