How can I get a list of all possible partitions of a vector in R?

后端 未结 2 694
清歌不尽
清歌不尽 2021-01-05 07:42

Suppose I have an R vector of unique elements such as x <- c(1,2,3,4,5).

Is there a function to give me a list of all possible partitions of this vec

2条回答
  •  耶瑟儿~
    2021-01-05 08:46

    Does this give you what you are looking for,

    install.packages("gregmisc", dependencies = TRUE)
    library(gregmisc)
    
    x <- c(1,2,3,4,5)
    for(i in 1:length(x)) {
    print(combinations(5,i,x,repeats=TRUE))
    }
    

提交回复
热议问题