-
Notifications
You must be signed in to change notification settings - Fork 22
Open
Description
cumsum
tolerates empty elements in a RleList
unless the empty element is in terminal position…
> RleList(Rle(), Rle(1), Rle(1)) |> cumsum()
RleList of length 3
[[1]]
numeric-Rle of length 0 with 0 runs
Lengths:
Values :
[[2]]
numeric-Rle of length 1 with 1 run
Lengths: 1
Values : 1
[[3]]
numeric-Rle of length 1 with 1 run
Lengths: 1
Values : 1
> RleList(Rle(1), Rle(), Rle(1)) |> cumsum()
RleList of length 3
[[1]]
numeric-Rle of length 1 with 1 run
Lengths: 1
Values : 1
[[2]]
numeric-Rle of length 0 with 0 runs
Lengths:
Values :
[[3]]
numeric-Rle of length 1 with 1 run
Lengths: 1
Values : 1
> RleList(Rle(1), Rle(1), Rle()) |> cumsum()
Error: subscript contains out-of-bounds indices
I solve the problem as follows in my package but it is a bit ugly.
> safe_cumsum
function(x) {
x <- c(x, Rle(1))
cs <- cumsum(x)
head(cs, -1)
}
<bytecode: 0x563e22a916f0>
> RleList(Rle(1), Rle(1), Rle()) |> safe_cumsum()
RleList of length 3
[[1]]
numeric-Rle of length 1 with 1 run
Lengths: 1
Values : 1
[[2]]
numeric-Rle of length 1 with 1 run
Lengths: 1
Values : 1
[[3]]
numeric-Rle of length 0 with 0 runs
Lengths:
Values :
It would be nice if cumsum
would tolerate lists ending with an empty element.
Metadata
Metadata
Assignees
Labels
No labels