R对于列表生成因子的所有组合,合并的所有组合以及组合

Sup*_*123 5 reduce merge combinations split r

所以我正在处理癌症阶段数据.假设这种类型的数据集.它是一个数据帧.

   cancertype     stage
TCGA-67-6215-01     1
TCGA-67-6216-01     1
TCGA-67-6217-01     2
TCGA-69-7760-01     2
TCGA-69-7761-01     1
TCGA-69-7763-01     1
TCGA-69-7764-01     1
TCGA-69-7765-01     4
TCGA-69-7980-01     1
TCGA-71-6725-01     1
TCGA-73-4658-01     1
TCGA-73-4659-01     3
TCGA-73-4662-01     1
TCGA-73-4675-01     3
Run Code Online (Sandbox Code Playgroud)

所以我想要的是一个列表,其中每个元素都是一个数据帧.这里有4个可能的癌症阶段.应该有2个级别,3个级别等的每个组合的数据帧,直到数据中的级别数.但也是合并级别的每个组合的数据帧.我的意思是

list(
dataframe of stage1 and 2
dataframe of stage1 and 3
dataframe of stage 1 and 4
dataframe of stage 2 and 3
...etc
dataframe of stage 1,2 and 3
dataframe of stage 2,3 and 4
...
dataframe of stage 1,2 and 3,4
dataframe of stage 1,3 and 2,4
dataframe of stage 1,2,3 and 4
dataframe of stage 1,2,4 and 3
.. etc etc I think this should give you the idea.
)
Run Code Online (Sandbox Code Playgroud)

在这里,当我说第1,2,4阶段时,我的意思是它们都被合并到一个级别.

我基本上是在尝试对t检验进行所有可能的比较,所以我正在设置我需要进行此比较的样本.只做每一个可能的组合和合并组合会很高兴.

到目前为止,我能够将未合并比较的所有元素组合在一起,即11.即2个阶段的6个组合,3个阶段的4个组合,4个阶段的1个组合

stage # dataframe of stage data as factors
stage_split <-split(stage,stage[,1])
allcombos<- c(combn(stage_split,2,simplify=F), combn(stage_split,3,simplify=F), combn(stage_split,4,simplify=F))
allcombos_cmbnd<- lapply(allcombos, function(x) Reduce(rbind,x))
Run Code Online (Sandbox Code Playgroud)

如何从所有可能的合并排列中生成其他数据帧,然后附加到此列表?也许从第一个数据帧有一个优雅的方式来实现这一目标.一种方法是迭代这个11的列表并从3的组合开始生成合并?我可以强制它,但我希望有一个优雅的方式来执行这可以扩大规模.到目前为止,我找不到任何解释如何生成数据中所有级别组合以及所有级别的合并组合.

谢谢你的帮助

pic*_*ick 2

当您将阶段分组在一起时,您正在对大小为 3 或 4 的集进行分区。有一个包,partitions可以使用 实现集分区setparts。在这里,我重点关注合并部分,因为听起来您已经弄清楚了非合并分组。

 ## For unmerged, get groupings with something like this
combos <- unlist(lapply(2:4, function(x) combn(unique(dat$stage), x, simplify=F)), rec=F)

## For merged groupings, use set partitioning
library(partitions)
dats <- unlist(sapply(3:4, function(p) {
    parts <- setparts(p)  # set partitions of size p
    lst <- lapply(split(parts, col(parts)), function(idx) {
        if (p==3) {       # with sets of 3, need to exclude one of the stages
            subLst <- lapply(1:4, function(exclude) {
                tmp <- dat$stage
                tmp[dat$stage==exclude] <- NA
                ids <- seq(4)[-exclude]
                for (i in 1:3) tmp[dat$stage==ids[i]] <- idx[i]
                data.frame(dat$cancertype, stage=tmp)
            })
            names(subLst) <- paste(1:4)
            subLst
        } else {          # sets of 4, no need to exclude
            tmp <- dat$stage
            for (i in 1:length(idx)) tmp[dat$stage==i] <- idx[i]
            data.frame(dat$cancertype, stage=tmp)
        }
    })
    names(lst) <- lapply(split(parts, col(parts)), paste, collapse=".")
    lst
}), rec=F)
Run Code Online (Sandbox Code Playgroud)

datsdata.frames现在是按stage设置分区分组的 s的列表。当划分大小为 3 的集合时,必须删除其中一个阶段。因此,这些条目dats显示为长度为 4 的列表,每个元素对应于从考虑中删除一个阶段(列表是有序的,因此第一个组件删除阶段 1,第二个组件删除阶段 2,等等)。让我们看一下一个或大小为 3 的分区,

dats[4]
$`2.1.1`
# $`2.1.1`$`1`
#     dat.cancertype stage
# 1  TCGA-67-6215-01    NA
# 2  TCGA-67-6216-01    NA
# 3  TCGA-67-6217-01     2
# 4  TCGA-69-7760-01     2
# 5  TCGA-69-7761-01    NA
# 6  TCGA-69-7763-01    NA
# 7  TCGA-69-7764-01    NA
# 8  TCGA-69-7765-01     1
# 9  TCGA-69-7980-01    NA
# 10 TCGA-71-6725-01    NA
# 11 TCGA-73-4658-01    NA
# 12 TCGA-73-4659-01     1
# 13 TCGA-73-4662-01    NA
# 14 TCGA-73-4675-01     1
# 
# $`2.1.1`$`2`
#     dat.cancertype stage
# 1  TCGA-67-6215-01     2
# 2  TCGA-67-6216-01     2
# 3  TCGA-67-6217-01    NA
# 4  TCGA-69-7760-01    NA
# 5  TCGA-69-7761-01     2
# 6  TCGA-69-7763-01     2
# 7  TCGA-69-7764-01     2
# 8  TCGA-69-7765-01     1
# 9  TCGA-69-7980-01     2
# 10 TCGA-71-6725-01     2
# 11 TCGA-73-4658-01     2
# 12 TCGA-73-4659-01     1
# 13 TCGA-73-4662-01     2
# 14 TCGA-73-4675-01     1
Run Code Online (Sandbox Code Playgroud)

这里的命名约定是group1.group2.group3$excludedGroup,相同的数字表示组已合并。因此,2.1.1$1意味着第一组已被排除($1,实际上只是转换为NA),并且在剩余数据中,第 2 组和第 3 组已合并。这有点令人困惑,可能需要更好的命名方案。例如,$2.1.1$1意味着“阶段 1 被排除 (NA),阶段 3 和阶段 4 已合并”。因此,我可以使用dats[['2.1.1']][['1']]. 此列表中还有两个未显示的 data.frame(不包括阶段 3 和阶段 4)。

现在,set-4 分区更加简单,因为没有排除项。例如,

dats[19]
# $`2.3.1.1`
#     dat.cancertype stage
# 1  TCGA-67-6215-01     2
# 2  TCGA-67-6216-01     2
# 3  TCGA-67-6217-01     3
# 4  TCGA-69-7760-01     3
# 5  TCGA-69-7761-01     2
# 6  TCGA-69-7763-01     2
# 7  TCGA-69-7764-01     2
# 8  TCGA-69-7765-01     1
# 9  TCGA-69-7980-01     2
# 10 TCGA-71-6725-01     2
# 11 TCGA-73-4658-01     2
# 12 TCGA-73-4659-01     1
# 13 TCGA-73-4662-01     2
# 14 TCGA-73-4675-01     1
Run Code Online (Sandbox Code Playgroud)

这里的命名是“Group1.Group2.Group3.Group4”。例如,在此分组阶段 3 和 4 已合并(两者 == 1)。

这里有冗余,您可以使用分区集或大小 3 的排除或分区集大小 4 并对每个进行多重比较data.frame。例如,在上面显示的数据集中,可以使用dats[['2.3.1.1']]或两者dats[['2.1.1']][['1']]并dats[['2.1.1']][['2']]结合来完成等效测试。

为了简化事情,data.frame您可以只存储索引,或者只在循环中进行计算,而不是将所有这些存储在列表中。