r中的fill.gaps()不会填补我的数据中的所有空白

发布于 2025-02-13 16:02:37 字数 640 浏览 1 评论 0 原文

我有一个带有两列的tibble:

library(tibble)
data <- tibble(yearweek = c("2020 W25", "2020 W33"),
               qty = c(100, 150))

自2015年W53以来,我需要添加整个星期,直到当前一周,所以我使用

data <- data %>%
    fill_gaps(qty = 0,
              .start = yearweek("2016-01-01"),
              .end = yearweek(Sys.time()),
              .full = TRUE)

所产生的数据不是我需要的,因为它仍然有差距(每年都有一个星期的时间) 8)。

我想念什么?我想做的做我想做的事的 fill_gaps()是否有任何选项?

编辑我认为这种行为的原因是丢失了“年度周”索引的清晰完整索引,并且命令无法获得全职范围。那么,是否可以使用索引值的完整向量来填充Tibble?

I have a tibble with two columns:

library(tibble)
data <- tibble(yearweek = c("2020 W25", "2020 W33"),
               qty = c(100, 150))

I need to add all the week since 2015 W53 until the current week, so I use

data <- data %>%
    fill_gaps(qty = 0,
              .start = yearweek("2016-01-01"),
              .end = yearweek(Sys.time()),
              .full = TRUE)

The data produced are not what I need because it still have gaps in the yearweek value (it fills like a week every 8).

What did I miss? Is there any options of the fill_gaps() that I miss to do what I want?

EDIT I think the cause of the behaviour is that a clear full index of the yearweek index is missing and the command does not get the full time range. So is there a way to use a full vector of the index value to fill the tibble?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多彩岁月 2025-02-20 16:02:37

将W34添加到数据中,导致没有空白。

将W35添加到数据中会导致替代数周。

(似乎是根据数据中的最小时间步长计算的暗示丢失的周。)

library(tidyverse)
library(tsibble)

# Fill gaps gives consecutive weeks with W34
data <- tibble(
  yearweek = c("2020 W25", "2020 W33", "2020 W34"),
  qty = c(100, 150, 200)
)

data %>%
  mutate(yearweek = yearweek(yearweek)) %>%
  tsibble() %>%
  fill_gaps(
    qty = 0,
    .start = yearweek("2015 W53"),
    .end = yearweek(Sys.time()),
    .full = TRUE
  )
#> Using `yearweek` as index variable.
#> # A tsibble: 341 x 2 [1W]
#>    yearweek   qty
#>      <week> <dbl>
#>  1 2015 W53     0
#>  2 2016 W01     0
#>  3 2016 W02     0
#>  4 2016 W03     0
#>  5 2016 W04     0
#>  6 2016 W05     0
#>  7 2016 W06     0
#>  8 2016 W07     0
#>  9 2016 W08     0
#> 10 2016 W09     0
#> # … with 331 more rows

# Fill gaps gives alternate weeks with W35
data <- tibble(
  yearweek = c("2020 W25", "2020 W33", "2020 W35"),
  qty = c(100, 150, 200)
)

data %>%
  mutate(yearweek = yearweek(yearweek)) %>%
  tsibble() %>%
  fill_gaps(
    qty = 0,
    .start = yearweek("2015 W53"),
    .end = yearweek(Sys.time()),
    .full = TRUE
  )
#> Using `yearweek` as index variable.
#> # A tsibble: 174 x 2 [2W]
#>    yearweek   qty
#>      <week> <dbl>
#>  1 2015 W53     0
#>  2 2016 W02     0
#>  3 2016 W04     0
#>  4 2016 W06     0
#>  5 2016 W08     0
#>  6 2016 W10     0
#>  7 2016 W12     0
#>  8 2016 W14     0
#>  9 2016 W16     0
#> 10 2016 W18     0
#> # … with 164 more rows

。 “ nofollow noreferrer”> reprex软件包(v2.0.1)

Adding a W34 to the data results in no gaps.

Adding a W35 to the data results in alternate weeks.

(It seems to be calculating implied missing weeks based on the smallest time-step in the data.)

library(tidyverse)
library(tsibble)

# Fill gaps gives consecutive weeks with W34
data <- tibble(
  yearweek = c("2020 W25", "2020 W33", "2020 W34"),
  qty = c(100, 150, 200)
)

data %>%
  mutate(yearweek = yearweek(yearweek)) %>%
  tsibble() %>%
  fill_gaps(
    qty = 0,
    .start = yearweek("2015 W53"),
    .end = yearweek(Sys.time()),
    .full = TRUE
  )
#> Using `yearweek` as index variable.
#> # A tsibble: 341 x 2 [1W]
#>    yearweek   qty
#>      <week> <dbl>
#>  1 2015 W53     0
#>  2 2016 W01     0
#>  3 2016 W02     0
#>  4 2016 W03     0
#>  5 2016 W04     0
#>  6 2016 W05     0
#>  7 2016 W06     0
#>  8 2016 W07     0
#>  9 2016 W08     0
#> 10 2016 W09     0
#> # … with 331 more rows

# Fill gaps gives alternate weeks with W35
data <- tibble(
  yearweek = c("2020 W25", "2020 W33", "2020 W35"),
  qty = c(100, 150, 200)
)

data %>%
  mutate(yearweek = yearweek(yearweek)) %>%
  tsibble() %>%
  fill_gaps(
    qty = 0,
    .start = yearweek("2015 W53"),
    .end = yearweek(Sys.time()),
    .full = TRUE
  )
#> Using `yearweek` as index variable.
#> # A tsibble: 174 x 2 [2W]
#>    yearweek   qty
#>      <week> <dbl>
#>  1 2015 W53     0
#>  2 2016 W02     0
#>  3 2016 W04     0
#>  4 2016 W06     0
#>  5 2016 W08     0
#>  6 2016 W10     0
#>  7 2016 W12     0
#>  8 2016 W14     0
#>  9 2016 W16     0
#> 10 2016 W18     0
#> # … with 164 more rows

Created on 2022-07-07 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文