Generate all possible permutations (or n-tuples)

前端 未结 2 1944
盖世英雄少女心
盖世英雄少女心 2020-12-01 16:12

I\'d like to create a data.frame of all possible permutations of 10 variables that can be either 1 or 2

2*2*2*2*2*2*2*2*2*2 = 1024 # possible

1,1,1,1,1,1,1,         


        
相关标签:
2条回答
  • 2020-12-01 16:23

    how about this:

    tmp = expand.grid(1:2,1:2,1:2,1:2,1:2,1:2,1:2,1:2,1:2,1:2)
    

    or this (thanks Tyler):

    x <- list(1:2)
    tmp = expand.grid(rep(x, 10))
    
    0 讨论(0)
  • 2020-12-01 16:39

    Some people have asked the question regarding letters, such as here. The expand.grid solution is usually given, but I find this to be much simpler:

    sapply(LETTERS[1:3], function(x){paste0(x, LETTERS[1:3])}) %>% c()
    # [1] "AA" "AB" "AC" "BA" "BB" "BC" "CA" "CB" "CC"
    
    0 讨论(0)
提交回复
热议问题