r语言 - 间隔外的日期的按组选择性总和



这个问题是这里提出的问题的变体。

我有以下类型的数据:

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

我正在根据变量 a、b 和 c(即data %>% group_by(a, b, c)).对于每个组,具有去年真正开始日期的行是感兴趣的。如果 strt 不大于 strt 并且小于或等于组中任何其他行的 fnsh,则该 strt 为真值。因此,即使组中有另一个具有相同值的 strt,一个 strt 也可以是真实的。

挑战在于对每个组中的真实strt进行选择性的总和。在计算此总和时,一个组中相同的真实strt的集合应算作一个。

以下标识了真正的开始日期,但未提供总和:

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

以上返回:

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

需要的是这样的:

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

如何为将一组相同的真实strts视为一个的组做一个总和?

任务是计算唯一真实日期的数量。我们可以在strt的滤波向量上使用n_distinctn_distinct(strt[genuine])

请注意,我放弃了genuine列的类型转换(在您的数据中称为begin(,因为之后我必须重新转换为逻辑。

希望这有帮助:

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

创建于 2020-06-18 由 reprex 软件包 (v0.3.0(

数据:

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

最新更新