0

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

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()

谢谢你的帮助!

4

5 回答 5

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
于 2017-08-03T11:12:02.433 回答
1

我不确定您希望如何格式化结果,因此我停止获取排除两个值出现的组合。它依赖于返回一个矩阵的事实,combn其中每列都是一个组合。

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"))
于 2017-08-03T11:03:11.543 回答
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]
    })
}
于 2017-08-03T11:12:58.227 回答
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()
于 2017-08-03T15:53:15.140 回答
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"))
于 2021-01-27T03:15:10.263 回答