Skip to content

Instantly share code, notes, and snippets.

@acvill
Last active June 6, 2023 21:02
Show Gist options
  • Save acvill/6f611989d8fc1f663b2cd91d94c95dfb to your computer and use it in GitHub Desktop.
Save acvill/6f611989d8fc1f663b2cd91d94c95dfb to your computer and use it in GitHub Desktop.
extract 1-based indices from a circular sequence within a range of a given index
getrange <- function(i, length, range){
sort(c(
c(((i - 1) - c(1:range)) %% length + 1),
i,
c(((i - 1) + c(1:range)) %% length + 1)
))
}
## example: get a vector of indices +/- 10 around i = 5 on the circular integer interval [1,100]
getrange(i = 5, length = 100, range = 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment