间隔外的分组选择性日期总和
Group-wise selective sum of dates outside intervals
这个问题是 问题的变体。
我有以下类型的数据:
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))
进行分组操作。对于每个组,真正开始日期在去年的行很重要。A strt如果它不大于组中任何其他行的 strt 且小于或等于 fnsh,则它是真实的。因此,即使组中有另一个具有相同值的 strt,strt 也可以是真实的。
挑战在于对每个组中的真实 strts 进行选择性求和。在计算总和时,一组中相同的真正 strts 的集合应该算作一个。
以下是真正的开始日期,但未提供总和:
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')))
以上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
需要的是:
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_distinct
:n_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
由 reprex package (v0.3.0)
于 2020-06-18 创建
数据:
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')))
这个问题是
我有以下类型的数据:
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))
进行分组操作。对于每个组,真正开始日期在去年的行很重要。A strt如果它不大于组中任何其他行的 strt 且小于或等于 fnsh,则它是真实的。因此,即使组中有另一个具有相同值的 strt,strt 也可以是真实的。
挑战在于对每个组中的真实 strts 进行选择性求和。在计算总和时,一组中相同的真正 strts 的集合应该算作一个。
以下是真正的开始日期,但未提供总和:
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')))
以上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
需要的是:
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_distinct
:n_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
由 reprex package (v0.3.0)
于 2020-06-18 创建数据:
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')))