gpt4 book ai didi

r - 变异 : chose all rows except the current one in a grouped df (dplyr)

转载 作者:行者123 更新时间:2023-12-04 10:38:08 25 4
gpt4 key购买 nike

我想标记以下重叠:同一团队中恢复时间间隔重叠的球员。

这是我的代码:

library(tidyverse)
library(lubridate)


# data
df<- data.frame(times=c("01/01/2020","01/02/2020","01/07/2020","01/10/2020","01/01/2020","01/03/2020","01/05/2020","01/07/2020"))
starttime= mdy(df$times)


df <- tibble(team=c("A","A","A","A","B","B","B","B"),
player=c(1,2,3,4,1,2,3,4),
starttime= starttime,
stoptime= starttime+days(1))


# find overlaps
df %>%
mutate(interval=lubridate::interval(starttime,stoptime)) %>% #calculate interval
group_by(team) %>%
mutate(overlap_flag= case_when(
sum(starttime %within% as.list(interval)) == 0 ~ 0, # I want to chose as.list(interval[except actual row])
sum(starttime %within% as.list(interval)) > 0 ~ 1, # I want to chose as.list(interval[except actual row])
TRUE ~ NA_real_))

给我:
# A tibble: 8 x 6
# Groups: team [2]
team player starttime stoptime interval overlap_flag
<chr> <dbl> <date> <date> <Interval> <dbl>
1 A 1 2020-01-01 2020-01-02 2020-01-01 UTC--2020-01-02 UTC 1
2 A 2 2020-01-02 2020-01-03 2020-01-02 UTC--2020-01-03 UTC 1
3 A 3 2020-01-07 2020-01-08 2020-01-07 UTC--2020-01-08 UTC 1
4 A 4 2020-01-10 2020-01-11 2020-01-10 UTC--2020-01-11 UTC 1
5 B 1 2020-01-01 2020-01-02 2020-01-01 UTC--2020-01-02 UTC 1
6 B 2 2020-01-03 2020-01-04 2020-01-03 UTC--2020-01-04 UTC 1
7 B 3 2020-01-05 2020-01-06 2020-01-05 UTC--2020-01-06 UTC 1
8 B 4 2020-01-07 2020-01-08 2020-01-07 UTC--2020-01-08 UTC 1

我想要的是:
# A tibble: 8 x 6
# Groups: team [2]
team player starttime stoptime interval overlap_flag
<chr> <dbl> <date> <date> <Interval> <dbl>
1 A 1 2020-01-01 2020-01-02 2020-01-01 UTC--2020-01-02 UTC 1
2 A 2 2020-01-02 2020-01-03 2020-01-02 UTC--2020-01-03 UTC 1
3 A 3 2020-01-07 2020-01-08 2020-01-07 UTC--2020-01-08 UTC 0
4 A 4 2020-01-10 2020-01-11 2020-01-10 UTC--2020-01-11 UTC 0
5 B 1 2020-01-01 2020-01-02 2020-01-01 UTC--2020-01-02 UTC 0
6 B 2 2020-01-03 2020-01-04 2020-01-03 UTC--2020-01-04 UTC 0
7 B 3 2020-01-05 2020-01-06 2020-01-05 UTC--2020-01-06 UTC 0
8 B 4 2020-01-07 2020-01-08 2020-01-07 UTC--2020-01-08 UTC 0

我知道可能有一个 data.table解决方案......但是,我想知道这是否可以通过 dplyr 轻松完成

最佳答案

我们可以使用 row_number()循环遍历行,然后将其用作删除“开始时间”值的索引

library(dplyr)
library(lubridate)
library(purrr)
df %>%
mutate(interval = as.list(interval(starttime, stoptime))) %>%
group_by(team) %>%
mutate(overlap_flag = +(map2_lgl(row_number(),
interval, ~ sum(starttime[-.x] %within% .y) > 0)))

关于r - 变异 : chose all rows except the current one in a grouped df (dplyr),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60068197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com