diff --git a/DESCRIPTION b/DESCRIPTION index 709b3e9..27e168d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: gpindex Title: Generalized Price and Quantity Indexes -Version: 0.6.1.9001 +Version: 0.6.1.9002 Authors@R: c( person("Steve", "Martin", role = c("aut", "cre", "cph"), email = "marberts@protonmail.com", diff --git a/NEWS.md b/NEWS.md index 1eba944..cabe717 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +## Version 0.6.2 + +- `splice_index()` now keeps names. + ## Version 0.6.1 - Updated maintainer email. diff --git a/R/splice.R b/R/splice.R index cf0fd9a..5275c58 100644 --- a/R/splice.R +++ b/R/splice.R @@ -77,8 +77,8 @@ splice_index <- function(x, periods = NULL, initial = NULL, published = FALSE) { } y <- lapply(x, \(z) rev(cumprod(rev(z)))[periods]) - res <- numeric(offset + length(x)) - res[seq_along(initial)] <- initial + # Use recursive = TRUE to keep names. + res <- c(initial, lapply(x, \(x) x[length(x)]), recursive = TRUE) iw <- seq.int(to = offset - 1L, length.out = n)[periods] if (published) { diff --git a/tests/Examples/gpindex-Ex.Rout.save b/tests/Examples/gpindex-Ex.Rout.save index 5a31498..b532116 100644 --- a/tests/Examples/gpindex-Ex.Rout.save +++ b/tests/Examples/gpindex-Ex.Rout.save @@ -501,12 +501,14 @@ Warning in back_period(period) : > # Use a movement splice to combine the indexes in each window > > splice_index(tg, 2) -[1] 1.391443 1.801142 2.230521 2.689833 + 2 3 4 5 +1.391443 1.801142 2.230521 2.689833 > > # ... or use a mean splice > > splice_index(tg) -[1] 1.391443 1.801142 2.228836 2.687826 + 2 3 4 5 +1.391443 1.801142 2.228836 2.687826 > > #---- Missing data ---- > diff --git a/tests/testthat/test-geks.R b/tests/testthat/test-geks.R index 8966278..9cca847 100644 --- a/tests/testthat/test-geks.R +++ b/tests/testthat/test-geks.R @@ -177,27 +177,36 @@ test_that("geks works with different splices", { splice_index( with(dat, tornqvist_geks(price, quantity, period, product, window = 7)) ), - c(1.06202143784605, 1.1203648908828, 1.17858384039388, 1.23760852899173, - 1.29782138422918, 1.35944797067126, 1.4190362067647, 1.48010601630221, - 1.5422495225642, 1.60541380403309, 1.66962623911985, 1.73494499443271) + setNames( + c(1.06202143784605, 1.1203648908828, 1.17858384039388, 1.23760852899173, + 1.29782138422918, 1.35944797067126, 1.4190362067647, 1.48010601630221, + 1.5422495225642, 1.60541380403309, 1.66962623911985, 1.73494499443271), + 2:13 + ) ) expect_equal( splice_index( with(dat, fisher_geks(price, quantity, period, product, window = 3)), periods = 1 ), - c(1.05490954999787, 1.11049864213464, 1.16678403299157, 1.22378841881023, - 1.28153702754999, 1.34005894105164, 1.3993875342448, 1.45956101762071, - 1.52062310501187, 1.58262383588933, 1.64562059096184, 1.70967935288461) + setNames( + c(1.05490954999787, 1.11049864213464, 1.16678403299157, 1.22378841881023, + 1.28153702754999, 1.34005894105164, 1.3993875342448, 1.45956101762071, + 1.52062310501187, 1.58262383588933, 1.64562059096184, 1.70967935288461), + 2:13 + ) ) expect_equal( splice_index( with(dat, jevons_geks(price, quantity, period, product, window = 6)), periods = 3 ), - c(1.18338442313092, 1.32033674805411, 1.43711591151299, 1.54248452241533, - 1.64048980305452, 1.73334427991861, 1.82239254133738, 1.90851390842145, - 1.99231538702512, 2.07423380909548, 2.15459410924166, 2.23364458259815) + setNames( + c(1.18338442313092, 1.32033674805411, 1.43711591151299, 1.54248452241533, + 1.64048980305452, 1.73334427991861, 1.82239254133738, 1.90851390842145, + 1.99231538702512, 2.07423380909548, 2.15459410924166, 2.23364458259815), + 2:13 + ) ) }) diff --git a/tests/testthat/test-splice.R b/tests/testthat/test-splice.R index dc9af7b..2375871 100644 --- a/tests/testthat/test-splice.R +++ b/tests/testthat/test-splice.R @@ -97,3 +97,8 @@ test_that("splicing is the same as chaining", { expect_equal(splice_index(x, initial = 1:3), cumprod(c(1:3, 1:5))) expect_equal(splice_index(x, published = TRUE), cumprod(1:5)) }) + +test_that("splicing keeps names", { + x <- list(1:3, c(a = 1, b = 2, c = 3), z = c(1:2, c = 3)) + expect_identical(names(splice_index(x)), c("", "", "", "c", "z.c")) +})