R:来自具有两个条件的元素向量的所有长度的所有组合 [英] R: all combinations of all lengths from a vector of elements each with 2 conditions

查看:74
本文介绍了R:来自具有两个条件的元素向量的所有长度的所有组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

紧随

Following up on this question I posted some days ago, I would like to extend the same case for combinations of every length.

所以我有一个向量形式:

So I have a vector of the form:

markers <- LETTERS[1:5]

最初,我只是希望所有 markers 的条件 + -的所有可能组合;即最低层级"5的组合.

Originally I just wanted all possible combinations of conditions + and - for all the markers; i.e "lowest hierarchy level" of combinations of 5.

因此将答案应用于上述问题,我得到了以下内容:

So applying the answer to the above question, I obtained the following:

 [1] "A+/B+/C+/D+/E+" "A-/B+/C+/D+/E+" "A+/B-/C+/D+/E+" "A-/B-/C+/D+/E+" "A+/B+/C-/D+/E+" "A-/B+/C-/D+/E+" "A+/B-/C-/D+/E+"
 [8] "A-/B-/C-/D+/E+" "A+/B+/C+/D-/E+" "A-/B+/C+/D-/E+" "A+/B-/C+/D-/E+" "A-/B-/C+/D-/E+" "A+/B+/C-/D-/E+" "A-/B+/C-/D-/E+"
[15] "A+/B-/C-/D-/E+" "A-/B-/C-/D-/E+" "A+/B+/C+/D+/E-" "A-/B+/C+/D+/E-" "A+/B-/C+/D+/E-" "A-/B-/C+/D+/E-" "A+/B+/C-/D+/E-"
[22] "A-/B+/C-/D+/E-" "A+/B-/C-/D+/E-" "A-/B-/C-/D+/E-" "A+/B+/C+/D-/E-" "A-/B+/C+/D-/E-" "A+/B-/C+/D-/E-" "A-/B-/C+/D-/E-"
[29] "A+/B+/C-/D-/E-" "A-/B+/C-/D-/E-" "A+/B-/C-/D-/E-" "A-/B-/C-/D-/E-"

现在,我想将此扩展到上层".1、2、3和4个 markers 的组合级别.所以我会得到类似的东西:

Now I want to extend this to "upper hierarchy" levels of combinations of 1, 2, 3, and 4 markers. So I would get something like:

"A+"
"A-"
"B+"
"B-"
"C+"
"C-"
...
"A+/B+"
"A-/B+"
"A+/B-"
"A-/B-"
"B+/C+"
"B+/C-"
"B-/C+"
"B-/C-"
...
"A+/B+/C+"
"A-/B+/C+"
...
"A+/B+/C+/D+/E+"
"A-/B+/C+/D+/E+"
"A+/B-/C+/D+/E+"
"A-/B-/C+/D+/E+"
"A+/B+/C-/D+/E+"
...

在对先前问题的公认答案之上构建最快的最佳方法是什么?

What would be the fastest optimal way to build on top of the accepted answer to the previous question?

不必一次完成,而是可以(甚至更好)来获得内部节点".来自5人一组的先前结果.也许正在研究 expand.grid 中间结果.

It doesn't have to be done in one shot, it would still be ok (or even better), to get the "inner nodes" from the previous results of groups of 5. Maybe working on the expand.grid intermediate result.

有什么主意吗?谢谢!

编辑

对我而言,最好的方法是实际上在较高层级组合中为所有标记保留一个占位符.

The best way for my intentions would be to actually keep a place holder for all the markers in the higher hierarchy combinations.

因此,在这种情况下,例如 A +/D-将变为 A +/NA/NA/D-/NA

So for example in this case A+/D- would become A+/NA/NA/D-/NA

编辑2

即使是从头开始创建所有可能的n尺寸组合(包括 NA )的第一个答案也非常好...在我的现实世界中,我有机会检索到一个更小的过滤器列表中的最低层级".5个标记"的组合.我会最感兴趣的.

Even the first answer to create all the possible n-size combinations (including NA) from scratch is really good... in my real world scenario I have the chance to retrieve a much smaller filtered list of the "lowest hierarchy level" combinations of 5 "markers" that I would be most interested in.

在这种情况下,可以选择提取上级节点"作为选择,这将是非常好的.筛选出的列表中的1,2,3,4 ... n(带有 NA )的组合(而不是从头开始生成所有可能的n尺寸组合)...

In this scenario, it would be really good to have the option to extract the "upper level nodes" of combinations of 1,2,3,4...n (with NA) from that filtered list (instead of generating all possible n-size combinations from scratch)...

有什么主意吗?

推荐答案

如果您仍然希望将NA值保留在其中,则可以将其视为与"+"值不同的值.或-",您也具有NA值.你可以做类似的事情

If you still wanted to keep the NA values in there, then just think of it as having a different value than "+" or "-", you just also have the NA value. You could do something like

markers <- LETTERS[1:5]

test <- expand.grid(lapply(seq(markers), function(x) c("+","-","NA")),stringsAsFactors=FALSE)

apply(test,1,function(x){paste0(ifelse(x=="NA", "NA", markers),ifelse(x=="NA","",x),collapse = "/")}) 

这篇关于R:来自具有两个条件的元素向量的所有长度的所有组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆