我正在使用以下代码:
library("partitions")
x <- c(2,4,6)
parts <- listParts(length(x))
out <- rapply(parts, function(ii) x[ii], how="replace")
计算所有分区的列表向量,但是我想使用k维的分区列表,例如:
k=2
{(2),(4,6)}{(4),(2,6)}{(6),(2,4)}
答案 0 :(得分:0)
也许有更好的方法可以做到这一点,但是以下方法可以满足您的需求。
library(partitions)
funParts <- function(x, k){
parts <- listParts(length(x))
res <- lapply(parts, function(inx) sapply(inx, function(i) x[i]))
res <- unlist(res, recursive = FALSE)
res <- res[sapply(res, length) <= k]
unique(res)
}
x <- c(2,4,6)
k <- 2
funParts(x, 2)
funParts(x, 1)
funParts(4:10, 3)