Permute all unique enumerations of a vector in R

前端 未结 7 795
忘了有多久
忘了有多久 2020-11-29 06:21

I\'m trying to find a function that will permute all the unique permutations of a vector, while not counting juxtapositions within subsets of the same element type.

7条回答
  •  无人及你
    2020-11-29 06:45

    Another option is the iterpc package, I believe it is the fastest of the existing method. More importantly, the result is in dictionary order (which may be somehow preferable).

    dat <- c(1, 0, 3, 4, 1, 0, 0, 3, 0, 4)
    library(iterpc)
    getall(iterpc(table(dat), order=TRUE))
    

    The benchmark indicates that iterpc is significant faster than all other methods described here

    library(multicool)
    library(microbenchmark)
    microbenchmark(uniqueperm2(dat), 
                   allPerm(initMC(dat)), 
                   getall(iterpc(table(dat), order=TRUE))
                  )
    
    Unit: milliseconds
                                         expr         min         lq        mean      median
                             uniqueperm2(dat)   23.011864   25.33241   40.141907   27.143952
                         allPerm(initMC(dat)) 1713.549069 1771.83972 1814.434743 1810.331342
     getall(iterpc(table(dat), order = TRUE))    4.332674    5.18348    7.656063    5.989448
              uq        max neval
       64.147399   74.66312   100
     1855.869670 1937.48088   100
        6.705741   49.98038   100
    

提交回复
热议问题