generate a matrix of all possible combinations of 5 numbers in n columns

There may be a more elegant solution:

n <- 5
expand.grid(rep(list(seq(0, n - 1)), 3))