在输出中,您将命名为具有相应计数的列表。但也许使用更清晰和直接的解决方案可以获得相同的结果。让我们拭目以待其他答案。
# data simulation
c1 <- sample(c(0,1), 10, replace=T)
c2 <- sample(c(0,1), 10, replace=T)
c3 <- sample(c(0,1), 10, replace=T)
c4 <- sample(c(0,1), 10, replace=T)
Counts <- sample(1:100, 10)
df <- data.frame(c1, c2,c3, c4, Counts)
f <- function(y) {
idx <- apply(df[,y, drop=F], 1, function(x) all(x == 1))
df[idx, "Counts"]
}
out <- list()
l <- sapply(1:4, function(x) {combn(x=c("c1", "c2", "c3", "c4"), m=x, simplify=F)})
l <- unlist(l, recursive=F)
for (x in l) {
out[[paste(x, collapse="")]] <- f(x)
}