R> set.seed(123)
R> data <- matrix(rnorm(6),3,10)
R> colnames(data) <- c("s1","s2","s3","s4","s5","s6","s7","s8","s9","s10")
R> print(data)
sorry, I don't know how to show the print
我想通过R包从数据框中获得所有可能的列组合?更少的时间更好
结果如下所示,
All possible two pair combination
column S1 and S2
column S2 and S3
column S3 and S4
...
all possible three pair combination
column S1, S2 and S3
column S2, S3 and S4
column S3, S4 and S5
...
答案 0 :(得分:1)
我已经做了一个功能,只要我需要它就会派上用场:
make_combinations <- function(x) {
l <- length(x)
mylist <- lapply(2:l, function(y) {
combn(x, y, simplify = FALSE)
})
mylist
}
results <- make_combinations(colnames(data))
results[[1]]
# [[1]]
# [1] "s1" "s2"
#
# [[2]]
# [1] "s1" "s3"
#
# [[3]]
# [1] "s1" "s4"
#
# [[4]]
# [1] "s1" "s5"
#
# [[5]]
# [1] "s1" "s6"
#
# [[6]]
# [1] "s1" "s7"
#and so on...
该函数输出一个列表,其中每个元素是另一个包含所有2路,3路,4路......组合的列表。在你的情况下,它有9个元素,从双向组合一直到10向组合。