Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cumsum on a RleList fails when the last element is empty. #115

Open
charles-plessy opened this issue Jul 18, 2023 · 0 comments
Open

cumsum on a RleList fails when the last element is empty. #115

charles-plessy opened this issue Jul 18, 2023 · 0 comments

Comments

@charles-plessy
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant