R-从给定约束的2个向量生成所有组合 [英] R - generate all combinations from 2 vectors given constraints

查看:96
本文介绍了R-从给定约束的2个向量生成所有组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在给定两个约束的情况下,我想生成两个向量的所有组合:第一个向量的字符数不能超过3个,第二个向量的字符数必须始终至少一个。我还想更改组合中字符的最终数量。

I would like to generate all combinations of two vectors, given two constraints: there can never be more than 3 characters from the first vector, and there must always be at least one characters from the second vector. I would also like to vary the final number of characters in the combination.

例如,这是两个向量:

vec1=c("A","B","C","D")
vec2=c("W","X","Y","Z")

说我想要组合三个字符。可能的可接受排列是: A B X A Y Z 。不能接受的排列是: A B C ,因为 vec2 。

Say I wanted 3 characters in the combination. Possible acceptable permutations would be: "A" "B" "X"or "A" "Y" "Z". An unacceptable permutation would be: "A" "B" "C" since there is not at least one character from vec2.

现在说我要组合5个字符。可能的可接受排列是: A C Z Y A Y Z X 。不能接受的排列是: A C D B X ,因为 vec2中有> 3个字符

Now say I wanted 5 characters in the combination. Possible acceptable permutations would be: "A" "C" "Z" "Y" or "A" "Y" "Z" "X". An unacceptable permutation would be: "A" "C" "D" "B" "X" since there are >3 characters from vec2.

我想我可以使用 expand.grid 生成所有组合,然后以某种方式子集,但必须有一种更简单的方法。

I suppose I could use expand.grid to generate all combinations and then somehow subset, but there must be an easier way. Thanks in advance!

推荐答案

我不确定这样做是否更容易,但是您可以放弃不满足您需求的排列方式此策略的条件:

I'm not sure wheter this is easier, but you can leave away permutations that do not satisfy your conditions whith this strategy:


  1. vec1 生成所有组合

vec2 生成所有可接受的组合。

generate all combinations from vec2 that are acceptable.

生成所有组合,并从1中提取一个解决方案。从2中提取一个解决方案。在此之后,我将使用条件3进行过滤。

generate all combinations taking one solution from 1. + one solution from 2. Here I'd do the filtering with condition 3 afterwards.

(如果您正在寻找组合,请完成,否则:)在每个结果中生成字母的所有排列。

(if you're looking for combinations, you're done, otherwise:) produce all permutations of letters within each result.

现在,让我们来

vec1 <- LETTERS [1:4]
vec2 <- LETTERS [23:26]

## lists can eat up lots of memory, so use character vectors instead.
combine <- function (x, y) 
  combn (y, x, paste, collapse = "")

res1 <- unlist (lapply (0:3, combine, vec1))
res2 <- unlist (lapply (1:length (vec2), combine, vec2))

现在我们有:

> res1
 [1] ""    "A"   "B"   "C"   "D"   "AB"  "AC"  "AD"  "BC"  "BD"  "CD"  "ABC"
[13] "ABD" "ACD" "BCD"
> res2
 [1] "W"    "X"    "Y"    "Z"    "WX"   "WY"   "WZ"   "XY"   "XZ"   "YZ"  
[11] "WXY"  "WXZ"  "WYZ"  "XYZ"  "WXYZ"

res3 <- outer (res1, res2, paste0)
res3 <- res3 [nchar (res3) == 5]

所以您在这里:

> res3
 [1] "ABCWX" "ABDWX" "ACDWX" "BCDWX" "ABCWY" "ABDWY" "ACDWY" "BCDWY" "ABCWZ"
[10] "ABDWZ" "ACDWZ" "BCDWZ" "ABCXY" "ABDXY" "ACDXY" "BCDXY" "ABCXZ" "ABDXZ"
[19] "ACDXZ" "BCDXZ" "ABCYZ" "ABDYZ" "ACDYZ" "BCDYZ" "ABWXY" "ACWXY" "ADWXY"
[28] "BCWXY" "BDWXY" "CDWXY" "ABWXZ" "ACWXZ" "ADWXZ" "BCWXZ" "BDWXZ" "CDWXZ"
[37] "ABWYZ" "ACWYZ" "ADWYZ" "BCWYZ" "BDWYZ" "CDWYZ" "ABXYZ" "ACXYZ" "ADXYZ"
[46] "BCXYZ" "BDXYZ" "CDXYZ" "AWXYZ" "BWXYZ" "CWXYZ" "DWXYZ"

如果您希望将结果分成单个字母:

If you prefer the results split into single letters:

res <- matrix (unlist (strsplit (res3, "")), nrow = length (res3), byrow = TRUE)
> res
      [,1] [,2] [,3] [,4] [,5]
 [1,] "A"  "B"  "C"  "W"  "X" 
 [2,] "A"  "B"  "D"  "W"  "X" 
 [3,] "A"  "C"  "D"  "W"  "X" 
 [4,] "B"  "C"  "D"  "W"  "X" 

(剪切)

[51,] "C"  "W"  "X"  "Y"  "Z" 
[52,] "D"  "W"  "X"  "Y"  "Z" 

以下是您的组合。

这篇关于R-从给定约束的2个向量生成所有组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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