r - 将重叠间隔与 lubridate 结合

标签 r dplyr lubridate

我希望合并润滑间隔,以便如果它们重叠,则从内部第一个时间获取最小值和从内部最后一个时间获取最大值并总结以创建一个跨越整个时间段的新间隔。这是一个reprex:

library(lubridate, warn.conflicts = FALSE)
library(dplyr, warn.conflicts = FALSE)
library(tibble)

dat <- tibble(
  animal = rep(c("elk", "wolf", "moose"), each = 2),
  date_interval = c(
    interval(as.Date("2020-04-01"), as.Date("2020-04-05")),
    interval(as.Date("2020-04-10"), as.Date("2020-04-15")),
    interval(as.Date("2020-03-01"), as.Date("2020-04-01")),
    interval(as.Date("2020-02-15"), as.Date("2020-03-15")),
    interval(as.Date("2020-10-01"), as.Date("2020-11-01")),
    interval(as.Date("2020-09-15"), as.Date("2020-10-15"))
  )
)

dat
#> # A tibble: 6 x 2
#>   animal date_interval                 
#>   <chr>  <Interval>                    
#> 1 elk    2020-04-01 UTC--2020-04-05 UTC
#> 2 elk    2020-04-10 UTC--2020-04-15 UTC
#> 3 wolf   2020-03-01 UTC--2020-04-01 UTC
#> 4 wolf   2020-02-15 UTC--2020-03-15 UTC
#> 5 moose  2020-10-01 UTC--2020-11-01 UTC
#> 6 moose  2020-09-15 UTC--2020-10-15 UTC

好的,在 wolfmoose水平,我们有重叠的间隔。假设这是同一个狼和驼鹿之类的东西会重复计算天数:
dat %>%
  group_by(animal) %>%
  mutate(time = time_length(date_interval)) %>%
  summarise(time_cumu = sum(time))
#> `summarise()` ungrouping output (override with `.groups` argument)
#> # A tibble: 3 x 2
#>   animal time_cumu
#>   <chr>      <dbl>
#> 1 elk       777600
#> 2 moose    5270400
#> 3 wolf     5184000
这是我想得到的总结重叠间隔的输出类型:
tibble(
  animal = c("elk", "elk", "wolf", "moose"),
  date_interval = c(
    interval(as.Date("2020-04-01"), as.Date("2020-04-05")),
    interval(as.Date("2020-04-10"), as.Date("2020-04-15")),
    interval(as.Date("2020-02-15"), as.Date("2020-04-01")),
    interval(as.Date("2020-09-15"), as.Date("2020-11-01"))
  )
)
#> # A tibble: 4 x 2
#>   animal date_interval                 
#>   <chr>  <Interval>                    
#> 1 elk    2020-04-01 UTC--2020-04-05 UTC
#> 2 elk    2020-04-10 UTC--2020-04-15 UTC
#> 3 wolf   2020-02-15 UTC--2020-04-01 UTC
#> 4 moose  2020-09-15 UTC--2020-11-01 UTC
想法?

最佳答案

lubridate 中似乎没有将区间向量合并为非重叠区间向量的函数。
这是实现它的一种方法:

int_merge <- function(x) {
  if(length(x) == 1) return(x)
  x <- x[order(int_start(x))]
  y <- x[1]
  for(i in 2:length(x)){
    if(int_overlaps(y[length(y)], x[i]))
      y[length(y)] <- interval(start = min(int_start(c(y[length(y)], x[i]))),
                               end = max(int_end(c(y[length(y)], x[i]))))
    else
      y <- c(y, x[i])
  }
  return(y)
}
这允许您执行以下操作:
dat %>% 
   group_by(animal) %>% 
   summarize(date_interval = int_merge(date_interval))

#> # A tibble: 4 x 2
#> # Groups:   animal [3]
#>   animal date_interval                 
#>   <chr>  <Interval>                    
#> 1 elk    2020-04-01 UTC--2020-04-05 UTC
#> 2 elk    2020-04-10 UTC--2020-04-15 UTC
#> 3 moose  2020-09-15 UTC--2020-11-01 UTC
#> 4 wolf   2020-02-15 UTC--2020-04-01 UTC

关于r - 将重叠间隔与 lubridate 结合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64653134/

相关文章:

r - 查找两个列表之间所有匹配项的索引

r - 使用confusioMatrix时如何解决 "The data cannot have more levels than the reference"错误?

r - 神秘的多边形问题导致错误的光栅化

r - 如何将 df 绑定(bind)到 df 列表

r - 从 R 中给定条件后的列 'n' 个字符中删除字符

r - 在lubridate中增加15个工作日

r - 选择上下匹配的N行

r - 将类似命名的不等长列表元素转换为数据框 R

r - 关于持续时间与周期如何在加月润滑中工作的混淆

r - 使用 ggplot 绘制具有不同起点和终点的时间序列数据时对月份间隔进行阴影处理