Skip to content

Instantly share code, notes, and snippets.

@enxt
Created October 5, 2019 14:52
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 enxt/92ca031c6f9c8a7cc3909a56a7fde39d to your computer and use it in GitHub Desktop.
Save enxt/92ca031c6f9c8a7cc3909a56a7fde39d to your computer and use it in GitHub Desktop.
Cluster vector into equal size groups
a <- runif(100, 0.01, 1)
o <- split(order(a), ceiling(seq_along(a)/20))
a <- cbind(a, g=NA)
for(i in 1:length(o)) { a[o[[i]], c('g')] <- i }
# visual test
library(ggplot2)
ggplot(as.data.frame(a), aes(x=seq_along(a), y=a, color=g)) + geom_point()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment