Skip to content

Instantly share code, notes, and snippets.

@MattSandy
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattSandy/29294795e93b720c6ac6 to your computer and use it in GitHub Desktop.
Save MattSandy/29294795e93b720c6ac6 to your computer and use it in GitHub Desktop.
Breaks a vector into defined segment lengths
#Define the interval to cycle through
interval <- 30
#Replace sample_vector with your vector
sample_vector <- runif(100, 1, 10)
#Loop through each break in the sequence
for(i in seq(1,length(sample_vector),interval)) {
#Used to prevent the sequence from extending beyond the remainders
if(i+interval>=length(sample_vector)) {
#Vector access for this sequence is as follows:
print(sample_vector[i:length(sample_vector)])
} else {
#Vector access for this sequence is as follows:
print(sample_vector[i:(i+(interval-1))])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment