Create all possible combinations of n elements with m levels


User 3354212

I'm looking for a short R script to create all possible combinations of 10 elements, each with the same three levels.

a <- letters[1:10]  # elements
b <- 1:3            # levels

The expected result is something like this

    a   b   c   d   e   f   g   h   i   j
    1   1   1   1   1   1   1   1   1   1
    1   1   1   1   1   1   1   1   1   2
    1   1   1   1   1   1   1   1   1   3
    1   1   1   1   1   1   1   1   2   1
    1   1   1   1   1   1   1   1   2   2
    1   1   1   1   1   1   1   1   2   3
    ......                                  
    3   3   3   3   3   3   3   3   3   3

where the first row is the title and the other rows are the combination. thank you for your help.

Li Zheyuan

It's surprising that you really need so many ( 3 ^ 10) combinations.

expand.grid(replicate(10, 1:3, simplify = FALSE))

Related


All possible combinations of PHP course levels

Muhammad Okefi I need to combine all courses entered by user, for example user add 5 courses, it will be this template: Course 1 Course 2 Course 3 Course 4 Course 5 After that I need to create a combination for all the courses with the grades array: array('A+'

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Create all possible combinations in PHP

Adam M So I'm trying to get a list of every possible combination of a set of words. Input text is "1, 2" and values are ("1" => "a", "b", "c") and ("2" => "d", "e") and I will get just Like: a, d b, d c, d a, e b, e c, e With the code I have now, I only get:

Create all possible combinations in PHP

Adam M So I'm trying to get a list of every possible combination of a set of words. Input text is "1, 2" and values are ("1" => "a", "b", "c") and ("2" => "d", "e") and I will get just Like: a, d b, d c, d a, e b, e c, e With the code I have now, I only get:

Create all possible combinations in PHP

Adam M So I'm trying to get a list of every possible combination of a set of words. Input text is "1, 2" and values are ("1" => "a", "b", "c") and ("2" => "d", "e") and I will get just Like: a, d b, d c, d a, e b, e c, e With the code I have now, I only get: