We can use expand.grid to create combinations in data.frame , convert to matrix and get the maximum value of each row rowMaxs from library(matrixStats) .
library(matrixStats) rowMaxs(as.matrix(expand.grid(rep(list(1:6),3)))) #[1] 1 2 3 4 5 6 2 2 3 4 5 6 3 3 3 4 5 6 4 4 4 4 5 6 5 5 5 5 5 6 6 6 6 6 6 6 2 #[38] 2 3 4 5 6 2 2 3 4 5 6 3 3 3 4 5 6 4 4 4 4 5 6 5 5 5 5 5 6 6 6 6 6 6 6 3 3 #[75] 3 4 5 6 3 3 3 4 5 6 3 3 3 4 5 6 4 4 4 4 5 6 5 5 5 5 5 6 6 6 6 6 6 6 4 4 4 #[112] 4 5 6 4 4 4 4 5 6 4 4 4 4 5 6 4 4 4 4 5 6 5 5 5 5 5 6 6 6 6 6 6 6 5 5 5 5 #[149] 5 6 5 5 5 5 5 6 5 5 5 5 5 6 5 5 5 5 5 6 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 #[186] 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
Or we can use pmax with expand.grid
do.call(pmax, expand.grid(rep(list(1:6),3)))
Or, as @Ben Bolker suggested, we can also use apply with MARGIN=1
apply(expand.grid(rep(list(1:6),3)),1,max)
Another option is outer with pmax .
c(outer(1:6, outer(1:6, 1:6, FUN=pmax), FUN= pmax))
Or outer with Vectorize d max
f1 <- function(x,y) max(x,y) c(outer(1:6, outer(1:6, 1:6, Vectorize(f1)), Vectorize(f1)))
akrun source share