Skip to content

Commit

Permalink
Fix difference() giving extra NA values for short vectors
Browse files Browse the repository at this point in the history
Resolves #310
  • Loading branch information
mitchelloharawild committed May 12, 2024
1 parent 9e0057e commit 12f95b8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Fix `difference()` giving extra NA values when the time series is shorter than the lag length. (#310, @mitchelloharawild)

# tsibble 1.1.4

* Fixed `vec_ptype2()` for `yearweek` and `yearquarter` for non-default week start. (#299)
Expand Down
2 changes: 1 addition & 1 deletion R/time-wise.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ difference <- function(x, lag = 1, differences = 1, default = NA,

diff_impl <- function(x, lag = 1, differences = 1, default = NA) {
diff_x <- diff(x, lag = lag, differences = differences)
vec_c(vec_rep(default, lag * differences), diff_x)
vec_c(vec_rep(default, pmin(vec_size(x), lag * differences)), diff_x)
}
5 changes: 5 additions & 0 deletions tests/testthat/test-timewise.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ test_that("difference() with `order_by`", {
# right <- mutate(scrambled, diff = difference(value, order_by = year)), msg)
# expect_equal(sort(right$diff, na.last = FALSE), difference(tsbl$value))
})


test_that("difference() with short time series (#310)", {
expect_length(difference(1, differences = 3), 1L)
})

0 comments on commit 12f95b8

Please sign in to comment.