#> combinations(3) # [,1] [,2] [,3] #[1,] 1 2 3 # #> combinations(2,1:3) # [,1] [,2] #[1,] 1 2 #[2,] 1 3 #[3,] 2 3 # #> combinations(2,c("A","B","C","D")) # [,1] [,2] #[1,] "A" "B" #[2,] "A" "C" #[3,] "A" "D" #[4,] "B" "C" #[5,] "B" "D" #[6,] "C" "D" # combinations <- function(x, string=1:x) { rbind(subsets(length(string), x, string)) } subsets <- function(n, r, s = 1:n) { if(mode(n) != "numeric" || length(n) != 1 || n < 1 || (n %% 1) != 0) stop("bad value of n") if(mode(r) != "numeric" || length(r) != 1 || r < 1 || (r %% 1) != 0) stop("bad value of r") if(!is.atomic(s) || length(s) < n) stop("s is either non-atomic or too short") fun <- function(n, r, s) if(r <= 0) vector(mode(s), 0) else if(r >= n) s[1:n] else rbind(cbind(s[1], Recall(n - 1, r - 1, s[-1])), Recall(n - 1, r, s[-1])) fun(n, r, s) }