【问题标题】:dplyr::complete/fill a time sequence, but only for limited stretches of timedplyr::complete/fill 时间序列,但仅限于有限的时间段
【发布时间】:2020-09-12 01:48:37
【问题描述】:

我正在尝试使用 dplyr::completefill 来填补动物体重时间序列中的空白(大部分时间大约每周称重),但我只想在一定范围内进行.

在以下示例数据集中,缺少几个日期:2020 年 1 月 29 日的一个称重日期和 3 月/4 月的一系列 4 个缺失周。我们可以忽略 1 周的称重(例如 1/29),并且可以在两周内“填充”原始重量,但不想再进一步了。第二组缺失数据只能再补13天,剩下的空缺应该是wt_g的NA。

library(tidyverse)
library(lubridate)

animalwts <- tibble::tribble(
      ~Animal,     ~WtDate, ~Wt_g,
      "A",  "1/1/2020",   20L,
      "A",  "1/8/2020",   21L,
      "A", "1/15/2020",   21L,
      "A", "1/22/2020",   23L,
      "A",  "2/5/2020",   25L,
      "A", "2/12/2020",   23L,
      "A", "2/19/2020",   24L,
      "A", "2/26/2020",   23L,
      "A",  "3/4/2020",   22L,
      "A",  "4/8/2020",   24L
    ) %>%
        mutate(WtDate = mdy(WtDate))

以下代码用于完成日期系列并填写所有缺失的数据

animalwts %>%
  group_by(Animal) %>%
  complete(WtDate = seq.Date(min(WtDate), max(WtDate), by = "day")) %>%
  fill(Wt_g) 

但我试图弄清楚如何complete 所有日期,但从任何给定日期起最多两周内只有fill 的权重,并为任何进一步丢失的数据放置 NA。

如果可能的话,我想留在“管道中”。

【问题讨论】:

  • This 可能会有所帮助。

标签: r dplyr time-series fill


【解决方案1】:

像这样?

library(tidyverse)
library(lubridate)

animalwts %>%
  group_by(Animal) %>%
  mutate(NA_lag = WtDate - lag(WtDate),
         last_measurement_date = WtDate) %>% 
  complete(WtDate = seq.Date(min(WtDate), max(WtDate), by = "day")) %>%
  fill(Wt_g) %>% 
  fill(last_measurement_date) %>% 
  group_by(last_measurement_date, NA_lag) %>% 
  mutate(days_missing = row_number()) %>% 
  mutate(Wt_g = if_else(days_missing > 14, NA_integer_, Wt_g))

数据

animalwts <- tibble::tribble(
  ~Animal,     ~WtDate, ~Wt_g,
  "A",  "1/1/2020",   20L,
  "A",  "1/8/2020",   21L,
  "A", "1/15/2020",   21L,
  "A", "1/22/2020",   23L,
  "A",  "2/5/2020",   25L,
  "A", "2/12/2020",   23L,
  "A", "2/19/2020",   24L,
  "A", "2/26/2020",   23L,
  "A",  "3/4/2020",   22L,
  "A",  "4/8/2020",   24L
) %>%
  mutate(WtDate = mdy(WtDate))

【讨论】:

    猜你喜欢
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多