Skip to content

Instantly share code, notes, and snippets.

@DavisVaughan
Created May 2, 2024 13:47
Show Gist options
  • Save DavisVaughan/45bbbd70623d698b2491bbfdbe359578 to your computer and use it in GitHub Desktop.
Save DavisVaughan/45bbbd70623d698b2491bbfdbe359578 to your computer and use it in GitHub Desktop.
hop-with-window-param
@DavisVaughan
Copy link
Author

library(slider)

x <- 1:20

window_parem <- c(1, 3, 1, rep(2, 17))

index <- seq_along(x)

# Start 1 value before the current value
starts <- index - 1L

# End `window_parem` values after the current value
stops <- index + window_parem

hop(x, starts, stops, identity)
#> [[1]]
#> [1] 1 2
#> 
#> [[2]]
#> [1] 1 2 3 4 5
#> 
#> [[3]]
#> [1] 2 3 4
#> 
#> [[4]]
#> [1] 3 4 5 6
#> 
#> [[5]]
#> [1] 4 5 6 7
#> 
#> [[6]]
#> [1] 5 6 7 8
#> 
#> [[7]]
#> [1] 6 7 8 9
#> 
#> [[8]]
#> [1]  7  8  9 10
#> 
#> [[9]]
#> [1]  8  9 10 11
#> 
#> [[10]]
#> [1]  9 10 11 12
#> 
#> [[11]]
#> [1] 10 11 12 13
#> 
#> [[12]]
#> [1] 11 12 13 14
#> 
#> [[13]]
#> [1] 12 13 14 15
#> 
#> [[14]]
#> [1] 13 14 15 16
#> 
#> [[15]]
#> [1] 14 15 16 17
#> 
#> [[16]]
#> [1] 15 16 17 18
#> 
#> [[17]]
#> [1] 16 17 18 19
#> 
#> [[18]]
#> [1] 17 18 19 20
#> 
#> [[19]]
#> [1] 18 19 20
#> 
#> [[20]]
#> [1] 19 20

hop_vec(x, starts, stops, sum, .ptype = integer())
#>  [1]  3 15  9 18 22 26 30 34 38 42 46 50 54 58 62 66 70 74 57 39

Created on 2024-05-02 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment