Skip to content

Commit c0cfb85

Browse files
Fix difference() giving extra NA values for short vectors (#311)
Resolves #310
1 parent 03e2f6f commit c0cfb85

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Fix `difference()` giving extra NA values when the time series is shorter than the lag length. (#310, @mitchelloharawild)
2+
13
# tsibble 1.1.4
24

35
* Fixed `vec_ptype2()` for `yearweek` and `yearquarter` for non-default week start. (#299)

R/time-wise.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ difference <- function(x, lag = 1, differences = 1, default = NA,
4343

4444
diff_impl <- function(x, lag = 1, differences = 1, default = NA) {
4545
diff_x <- diff(x, lag = lag, differences = differences)
46-
vec_c(vec_rep(default, lag * differences), diff_x)
46+
vec_c(vec_rep(default, pmin(vec_size(x), lag * differences)), diff_x)
4747
}

tests/testthat/test-timewise.R

+5
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ test_that("difference() with `order_by`", {
2121
# right <- mutate(scrambled, diff = difference(value, order_by = year)), msg)
2222
# expect_equal(sort(right$diff, na.last = FALSE), difference(tsbl$value))
2323
})
24+
25+
26+
test_that("difference() with short time series (#310)", {
27+
expect_length(difference(1, differences = 3), 1L)
28+
})

0 commit comments

Comments
 (0)