Determine all n-tuples using the elements of a set. This is really just a simple wrapper for expand.grid, so it is not optimized.
tuples(set, n = length(set), repeats = FALSE, list = FALSE)
set | a set |
---|---|
n | length of each tuple |
repeats | if set contains duplicates, should the result? |
list | tuples as list? |
a matrix whose rows are the n-tuples
tuples(1:2, 3)#> [,1] [,2] [,3] #> [1,] 1 1 1 #> [2,] 1 1 2 #> [3,] 1 2 1 #> [4,] 1 2 2 #> [5,] 2 1 1 #> [6,] 2 1 2 #> [7,] 2 2 1 #> [8,] 2 2 2tuples(1:2, 3, list = TRUE)#> [[1]] #> [1] 1 1 1 #> #> [[2]] #> [1] 1 1 2 #> #> [[3]] #> [1] 1 2 1 #> #> [[4]] #> [1] 1 2 2 #> #> [[5]] #> [1] 2 1 1 #> #> [[6]] #> [1] 2 1 2 #> #> [[7]] #> [1] 2 2 1 #> #> [[8]] #> [1] 2 2 2 #>#> [1] "xxx" "xxy" "xxz" "xyx" "xyy" "xyz" "xzx" "xzy" "xzz" "yxx" "yxy" "yxz" #> [13] "yyx" "yyy" "yyz" "yzx" "yzy" "yzz" "zxx" "zxy" "zxz" "zyx" "zyy" "zyz" #> [25] "zzx" "zzy" "zzz"# multinomial coefficients r <- 2 # number of variables, e.g. x, y n <- 2 # power, e.g. (x+y)^2 apply(burst(n,r), 1, function(v) factorial(n)/ prod(factorial(v))) # x, y, xy#> [1] 1 1 2#> x^2 + 2 x y + y^2r <- 2 # number of variables, e.g. x, y n <- 3 # power, e.g. (x+y)^3 apply(burst(n,r), 1, function(v) factorial(n)/ prod(factorial(v)))#> [1] 1 1 3 3#> x^3 + 3 x^2 y + 3 x y^2 + y^3r <- 3 # number of variables, e.g. x, y, z n <- 2 # power, e.g. (x+y+z)^2 apply(burst(n,r), 1, function(v) factorial(n)/ prod(factorial(v))) # x, y, z, xy, xz, yz#> [1] 1 1 1 2 2 2#> x^2 + 2 x y + 2 x z + y^2 + 2 y z + z^2