从R中的combn函数中排除某些组合

时间:2017-08-03 10:42:56

标签: r combn

我试图排除由“var4”和“var5”组成的combn函数生成的所有组合。以下是目前不起作用的代码:

mod_headers <- c("var1", "var2", "var3", "var4", "var5", "var6")

f <- function(){
  for(i in 1:length(mod_headers)){
    tab <- combn(mod_headers,i,function(mod_headers){
      if (combn(mod_headers,i) %in% c("var4","var5")) {return()}
    })
    for(j in 1:ncol(tab)){
      tab_new <- c(tab[,j])
      mod_tab_new <- c(tab_new, "newcol")
      print(mod_tab_new)
    }
  }
}

f()

感谢您的帮助!

5 个答案:

答案 0 :(得分:1)

我不确定您希望如何对结果进行格式化,因此我停止获取将两个值的外观排除在一起的组合。它依赖于mod_headers <- c("var1", "var2", "var3", "var4", "var5", "var6") combn_with_exclusion <- function(x, n, exclude){ full <- combn(x, n) # remove any columns that have all elements of `exclude` full[, !apply(full, 2, function(y) all(exclude %in% y))] } combn_with_exclusion(mod_headers, 2, c("var4", "var5")) 返回矩阵的事实,其中每列是一个组合。

{{1}}

答案 1 :(得分:1)

这是另一种方法,生成所有组合的列表,然后排除包含var4var5的组合......

lapply(
   lapply(1:length(mod_headers),
        function(i) combn(mod_headers, i)), 
   function(x) x[,apply(x, 2, function(y) !all(c("var4", "var5") %in% y))]) 

[[1]]
[1] "var1" "var2" "var3" "var4" "var5" "var6"

[[2]]
     [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]   [,8]   [,9]   [,10]  [,11]  [,12]  [,13]  [,14] 
[1,] "var1" "var1" "var1" "var1" "var1" "var2" "var2" "var2" "var2" "var3" "var3" "var3" "var4" "var5"
[2,] "var2" "var3" "var4" "var5" "var6" "var3" "var4" "var5" "var6" "var4" "var5" "var6" "var6" "var6"

[[3]]
     [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]   [,8]   [,9]   [,10]  [,11]  [,12]  [,13]  [,14]  [,15]  [,16] 
[1,] "var1" "var1" "var1" "var1" "var1" "var1" "var1" "var1" "var1" "var2" "var2" "var2" "var2" "var2" "var3" "var3"
[2,] "var2" "var2" "var2" "var2" "var3" "var3" "var3" "var4" "var5" "var3" "var3" "var3" "var4" "var5" "var4" "var5"
[3,] "var3" "var4" "var5" "var6" "var4" "var5" "var6" "var6" "var6" "var4" "var5" "var6" "var6" "var6" "var6" "var6"

...etc

答案 2 :(得分:0)

我只是在TIO上试过这个,所以没有基准测试,但是我敢打赌,对于大型套装来说这个版本会更快,如果这个版本重要的话。

m <- c("var2", "var3", "var4", "var5", "var6")
comb <- combn(m, 3)
csums <- colSums((comb == "var4") + (comb == "var5"))
comb[, csums < 2]
#      [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]  
# [1,] "var2" "var2" "var2" "var2" "var2" "var3" "var3"
# [2,] "var3" "var3" "var3" "var4" "var5" "var4" "var5"
# [3,] "var4" "var5" "var6" "var6" "var6" "var6" "var6"

或等同于OP的f()

f2 <- function(m=mod_headers) {
    lapply(1:length(m), function(x) {
      comb <- combn(m, x)
      csums <- colSums((comb == "var4") + (comb == "var5"))
      comb[, csums < 2]
    })
}

答案 3 :(得分:0)

这是我的解决方案:

f <- function(){
  for(i in 1:length(mod_headers)){
      tab <- combn(mod_headers,i)
      for(j in 1:ncol(tab)){
        tab_new <- c(tab[,j])
        mod_tab_new <- c(tab_new, "newcol")
        if (all(c("var4","var5") %in% mod_tab_new)) next
        print(mod_tab_new)
    }
  }
}

f()

答案 4 :(得分:0)

我使用这个网页来减少给定另一组 N-Way 组合的 N-Way 组合列表。这是对 Benjamins Code 的轻微修改。

mod_headers <- c("var1", "var2", "var3", "var4", "var5", "var6")

combn_NWayExclusion <- function(x, n, exclude){
   full <- combn(x, n); EXC<-combn(exclude, n)
   UU<-lapply(1:ncol(EXC),function(i) !apply(full, 2, function(y) all(EXC[,i] %in% y)))
   full[,!apply(do.call(rbind,UU),2,function(u){any(u=="FALSE")})]
   }

combn_NWayExclusion(mod_headers, 2, c("var4", "var5"))