【问题标题】:How to deduplicate date sequences across non-consecutive rows in R?如何在 R 中的非连续行中对日期序列进行重复数据删除?
【发布时间】:2020-12-29 23:30:18
【问题描述】:

我想在我的数据中为每个 id 标记至少 31 天的每个窗口中的第一个日期。
数据:

library(tidyverse)
library(lubridate)
library(tibbletime)

D1 <- tibble(id = c(12,12,12,12,12,12,10,10,10,10),
             index_date=c("2019-01-01","2019-01-07","2019-01-21","2019-02-02",
                    "2019-02-09","2019-03-06","2019-01-05","2019-02-01","2019-02-02","2019-02-08"))
D1

# A tibble: 10 x 2
      id index_date
   <dbl> <chr>     
 1    12 2019-01-01
 2    12 2019-01-07
 3    12 2019-01-21
 4    12 2019-02-02
 5    12 2019-02-09
 6    12 2019-03-06
 7    10 2019-01-05
 8    10 2019-02-01
 9    10 2019-02-02
10    10 2019-02-08

需要标记的行是第 1、4、6、7 和 10 行;这些行表示给定id 的第一个index_date 或从先前标记的index_date 为给定id 跳过31 天后的第一个index_date
代码:

temp <- D1 %>%
  mutate(index_date = ymd(index_date)) %>%  
  arrange(id, index_date) %>%
  as_tbl_time(index_date) %>% 
  group_by(id) %>%
  mutate(keyed_to_index_date = 
           collapse_index(index_date, period = '31 d', side = "start"),
           keep = index_date == keyed_to_index_date)
temp %>% arrange(desc(id), index_date)

结果:

      id index_date keyed_to_index_date keep       
   <dbl> <date>     <date>              <lgl>
 1    12 2019-01-01 2019-01-01          TRUE 
 2    12 2019-01-07 2019-01-01          FALSE
 3    12 2019-01-21 2019-01-01          FALSE
 4    12 2019-02-02 2019-02-02          TRUE 
 5    12 2019-02-09 2019-02-02          FALSE
 6    12 2019-03-06 2019-03-06          TRUE 
 7    10 2019-01-05 2019-01-05          TRUE 
 8    10 2019-02-01 2019-02-01          TRUE 
 9    10 2019-02-02 2019-02-01          FALSE
10    10 2019-02-08 2019-02-01          FALSE

为什么此代码会标记第 8 行(在之前标记 index_dateid 之后不到 31 天的 index_date)而不是第 10 行,我该如何解决这个问题?

更新:
按照@mnaR99 的建议,将选项start_date = first(index_date) 添加到collapse_index(),成功标记了原始示例中的正确行。但是,当我将相同的原理应用于新数据时,我遇到了一个问题:
数据:

D2 <- tibble(id = c("A","A","A","B","B","B","B","B","C","C","C"),
             index_date = c("2019-03-04","2019-03-05","2019-03-06",
                            "2019-03-01","2019-03-02","2019-03-04","2019-03-05","2019-03-06",
                            "2019-03-03","2019-03-04","2019-03-05"))

D2
   id    index_date
   <chr> <chr>     
 1 A     2019-03-04
 2 A     2019-03-05
 3 A     2019-03-06
 4 B     2019-03-01
 5 B     2019-03-02
 6 B     2019-03-04
 7 B     2019-03-05
 8 B     2019-03-06
 9 C     2019-03-03
10 C     2019-03-04
11 C     2019-03-05

我现在想以与之前应用 31 天窗口相同的方式应用 2 天窗口(也就是说,不应同时标记连续的日历日)。想要标记的行是第 1、3、4、6、8、9 和 11 行,因为这些行要么是特定 `id` 的第一个 `index_date`,要么是两天跳过后的第一个。
代码:
t3 <- D2 %>%
  mutate(index_date = ymd(index_date)) %>%  
  arrange(id, index_date) %>%
  as_tbl_time(index_date) %>% 
  group_by(id) %>%
  mutate(keyed_to_index_date = 
           collapse_index(index_date, 
                          period = '2 d', 
                          side = "start", 
                          start_date = first(index_date)),
         keep = index_date == keyed_to_index_date) %>%
  arrange(id, index_date)

结果:

> t3
# A time tibble: 11 x 4
# Index:  index_date
# Groups: id [3]
   id    index_date keyed_to_index_date keep 
   <chr> <date>     <date>              <lgl>
 1 A     2019-03-04 2019-03-04          TRUE 
 2 A     2019-03-05 2019-03-04          FALSE
 3 A     2019-03-06 2019-03-06          TRUE 
 4 B     2019-03-01 2019-03-01          TRUE 
 5 B     2019-03-02 2019-03-01          FALSE
 6 B     2019-03-04 2019-03-04          TRUE 
 7 B     2019-03-05 2019-03-05          TRUE 
 8 B     2019-03-06 2019-03-05          FALSE
 9 C     2019-03-03 2019-03-03          TRUE 
10 C     2019-03-04 2019-03-03          FALSE
11 C     2019-03-05 2019-03-05          TRUE 

第 7 行错误地标记为 TRUE,第 8 行错误地标记为 FALSE。
当我应用@tmfmnk 建议的purrr 解决方案时,我得到了正确的结果。
代码:

t4 <-
  D2 %>%
  group_by(id) %>%
  mutate(index_date = ymd(index_date),
         keep =  row_number() == 1 | 
           accumulate(c(0, diff(index_date)), ~ if_else(.x >= 2, 
                                                        .y, 
                                                        .x + .y)
           ) >= 2
  )

结果:

> t4
# A tibble: 11 x 3
# Groups:   id [3]
   id    index_date keep 
   <chr> <date>     <lgl>
 1 A     2019-03-04 TRUE 
 2 A     2019-03-05 FALSE
 3 A     2019-03-06 TRUE 
 4 B     2019-03-01 TRUE 
 5 B     2019-03-02 FALSE
 6 B     2019-03-04 TRUE 
 7 B     2019-03-05 FALSE
 8 B     2019-03-06 TRUE 
 9 C     2019-03-03 TRUE 
10 C     2019-03-04 FALSE
11 C     2019-03-05 TRUE

此示例中的 tibbletime 方法有什么问题?

【问题讨论】:

  • 合并@mnaR99 的答案并尝试应用于新数据

标签: r tidyr lubridate date-manipulation tibbletime


【解决方案1】:

您可以从purrr 使用accumulate()

D1 %>%
  group_by(id) %>% 
  mutate(index_date = ymd(index_date),
         keep = index_date == accumulate(index_date, ~ if(.y - .x >= 31) .y else .x))

#       id index_date keep 
#    <dbl> <date>     <lgl>
#  1    12 2019-01-01 TRUE 
#  2    12 2019-01-07 FALSE
#  3    12 2019-01-21 FALSE
#  4    12 2019-02-02 TRUE 
#  5    12 2019-02-09 FALSE
#  6    12 2019-03-06 TRUE 
#  7    10 2019-01-05 TRUE 
#  8    10 2019-02-01 FALSE
#  9    10 2019-02-02 FALSE
# 10    10 2019-02-08 TRUE

迭代规则如下:

1. 2019-01-07 -  2019-01-01   = 6  <  31 then return  2019-01-01
2. 2019-01-21 -  2019-01-01   = 20 <  31 then return  2019-01-01
3. 2019-02-02 -  2019-01-01   = 32 >= 31 then return (2019-02-02)*
4. 2019-02-09 - (2019-02-02)* = 7  <  31 then return  2019-02-02
5. etc.

【讨论】:

    【解决方案2】:

    您只需将start_date 参数添加到collapse_index

    D1 %>%
      mutate(index_date = ymd(index_date)) %>%  
      arrange(id, index_date) %>%
      as_tbl_time(index_date) %>% 
      group_by(id) %>%
      mutate(keyed_to_index_date = 
               collapse_index(index_date, period = '31 d', side = "start", start_date = first(index_date)),
               keep = index_date == keyed_to_index_date) %>% 
      arrange(desc(id), index_date)
    #> # A time tibble: 10 x 4
    #> # Index:  index_date
    #> # Groups: id [2]
    #>       id index_date keyed_to_index_date keep 
    #>    <dbl> <date>     <date>              <lgl>
    #>  1    12 2019-01-01 2019-01-01          TRUE 
    #>  2    12 2019-01-07 2019-01-01          FALSE
    #>  3    12 2019-01-21 2019-01-01          FALSE
    #>  4    12 2019-02-02 2019-02-02          TRUE 
    #>  5    12 2019-02-09 2019-02-02          FALSE
    #>  6    12 2019-03-06 2019-03-06          TRUE 
    #>  7    10 2019-01-05 2019-01-05          TRUE 
    #>  8    10 2019-02-01 2019-01-05          FALSE
    #>  9    10 2019-02-02 2019-01-05          FALSE
    #> 10    10 2019-02-08 2019-02-08          TRUE
    

    reprex package (v0.3.0) 于 2020-09-11 创建

    【讨论】:

    • 这适用于我的原始示例,但是当我在新示例上尝试相同的方法时,我没有得到正确的结果;查看更新的问题。
    【解决方案3】:

    使用dplyrlubridatepurrr 的一个选项可能是:

    D1 %>%
     group_by(id) %>%
     mutate(index_date = ymd(index_date),
            keep =  row_number() == 1 | accumulate(c(0, diff(index_date)), ~ if_else(.x >= 31, .y, .x + .y)) >= 31)
    
         id index_date keep 
       <dbl> <date>     <lgl>
     1    12 2019-01-01 TRUE 
     2    12 2019-01-07 FALSE
     3    12 2019-01-21 FALSE
     4    12 2019-02-02 TRUE 
     5    12 2019-02-09 FALSE
     6    12 2019-03-06 TRUE 
     7    10 2019-01-05 TRUE 
     8    10 2019-02-01 FALSE
     9    10 2019-02-02 FALSE
    10    10 2019-02-08 TRUE 
    

    【讨论】:

    • 这种方法在我在另一个示例上尝试时也有效(请参阅修改后的问题),但我最初尝试的 collapse_index() 方法(由@mnaR99 修改)在我尝试了一个新的例子。知道为什么吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 2017-09-20
    • 2011-02-13
    • 2022-09-27
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多