Skip to content

Instantly share code, notes, and snippets.

@phil8192
Last active September 14, 2018 12:13
Show Gist options
  • Save phil8192/2fb201f1c8322176776da9e37742933a to your computer and use it in GitHub Desktop.
Save phil8192/2fb201f1c8322176776da9e37742933a to your computer and use it in GitHub Desktop.
# desired matrix:
# 1 1
# 1 2
# 1 3
# 2 1
# 2 2
# 2 3
# 2 4
# 3 1
# 3 2
# 3 3
# 3 4
# 3 5
# probably a nicer way (as always). rle does the trick once more:
group_seq <- function(x.len=3:5, y.val=1:3) {
x <- inverse.rle(list(lengths=x.len, values=y.val))
y <- unlist(lapply(table(x), function(x) 1:x), use.names=F)
matrix(c(x, y), ncol=2)
}
# general form starting from 1, using indices of upper triangle of matrix (with diagonal)
# to simulate all combinations (with replacement)!
which(upper.tri(matrix(1:4^2,nrow=4),diag=T),arr.ind=T)[,2:1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment