[英]R For a list Generate all combinations of factor, all combinations of merges, and combine
所以我正在处理癌症阶段数据。 假设这种类型的数据集。 它是一个数据帧。
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
所以我想要的是一个列表,其中每个元素都是一个数据帧。 这里有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.
)
在这里,当我说第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))
如何从所有可能的合并排列中生成其他数据帧,然后附加到此列表? 也许从第一个数据帧有一个优雅的方式来实现这一目标。 一种方法是迭代这个11的列表并从3的组合开始生成合并? 我可以强制它,但我希望有一个优雅的方式来执行这可以扩大规模。 到目前为止,我找不到任何解释如何生成数据中所有级别组合以及所有级别的合并组合。
谢谢你的帮助
当您在分组阶段在一起,你是分区大小设置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)
dats
现在是列表data.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
这里的命名约定是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.frames未显示(不包括第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
这里的命名是“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
存储在列表中,而是只存储索引,或者只是在循环中进行计算。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.