Skip to content

Commit

Permalink
split(index) <- value now warns about recycling (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
marberts committed Sep 15, 2024
1 parent 86095f7 commit f555da1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: piar
Title: Price Index Aggregation
Version: 0.8.1
Version: 0.8.1.9001
Authors@R: c(
person("Steve", "Martin", role = c("aut", "cre", "cph"),
email = "[email protected]",
Expand Down
8 changes: 6 additions & 2 deletions R/split.piar_index.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#' all levels are kept.
#' @param margin Either 'levels' to split over the levels of `x` (the default),
#' or 'time' to split over the time periods of `x`.
#' @param value A list of values compatible with the splitting of `x`, recycled
#' if necessary.
#' @param value A list of values compatible with the splitting of `x`, or
#' something that can be coerced into one, recycled if necessary.
#' @param ... Further arguments passed to [`split.default()`].
#'
#' @returns
Expand Down Expand Up @@ -42,9 +42,13 @@ split.piar_index <- function(x, f, drop = FALSE, ...,
#' @export
`split<-.piar_index` <- function(x, f, drop = FALSE, ...,
margin = c("levels", "time"), value) {
value <- as.list(value)
margin <- match.arg(margin)
ix <- split(seq_along(x[[margin]]), f, drop = drop, ...)
n <- length(value)
if (n > 0L && length(ix) %% n != 0) {
warning("number of items to replace is not a multiple of replacement length")
}
j <- 0
if (margin == "levels") {
for (i in ix) {
Expand Down
4 changes: 2 additions & 2 deletions man/split.piar_index.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/testthat/test-split.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ test_that("replacement works", {
x <- as_index(matrix(1:6, 2))
split(x, c(1, 1, 2), margin = "time") <- 1:2
expect_equal(x, as_index(matrix(c(1, 1, 1, 1, 2, 2), 2)))

y <- x
split(y, factor(c(1, 1, 2), levels = character(0)), margin = "time") <- list()
expect_identical(x, y)

expect_warning(split(x, 1:3, margin = "time") <- 1:2)
})

0 comments on commit f555da1

Please sign in to comment.