R 问题 A,A,A,A,B,B,B,B,B 的唯一组合数

发布于 2024-11-10 07:01:49 字数 91 浏览 2 评论 0原文

我试图找到一种方法来获取 R 中 A、A、A、A、B、B、B、B、B 的所有可能的唯一排列的列表。

组合最初被认为是获得解决方案的方法,因此是组合答案。

I am trying to find a way to get a list in R of all the possible unique permutations of A,A,A,A,B,B,B,B,B.

Combinations was what was originally thought to be the method for obtaining a solution, hence the combinations answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹏半生如梦愿梦如真 2024-11-17 07:01:49

我想这就是你所追求的。 @bill 提出了将 uniquecombn 结合起来的建议。我们还将使用 apply 系列来生成所有组合。由于 unique 会删除重复行,因此我们需要在 unique 处理之前转置 combn 的结果。然后,我们在返回屏幕之前将它们转回原处,以便每一列代表一个唯一的答案。

#Daters
x <- c(rep("A", 4), rep("B",5))
#Generates a list with ALL of the combinations
zz <- sapply(seq_along(x), function(y) combn(x,y))
#Filter out all the duplicates
sapply(zz, function(z) t(unique(t(z))))

返回:

[[1]]
     [,1] [,2]
[1,] "A"  "B" 

[[2]]
     [,1] [,2] [,3]
[1,] "A"  "A"  "B" 
[2,] "A"  "B"  "B" 

[[3]]
     [,1] [,2] [,3] [,4]
[1,] "A"  "A"  "A"  "B" 
[2,] "A"  "A"  "B"  "B" 
[3,] "A"  "B"  "B"  "B" 

...

编辑 由于问题是关于排列而不是组合,因此上面的答案没有那么有用。 这篇文章概述了一个函数来生成给定的唯一排列一组参数。我不知道是否可以改进它,但这是使用该函数的一种方法:

fn_perm_list <-
 function (n, r, v = 1:n)
 {
    if (r == 1)
       matrix(v, n, 1)
    else if (n == 1)
       matrix(v, 1, r)
    else {
       X <- NULL
       for (i in 1:n) X <- rbind(X, cbind(v[i], fn_perm_list(n -
            1, r - 1, v[-i])))
        X
    }
 } 

zz <- fn_perm_list(9, 9)

#Turn into character matrix. This currently does not generalize well, but gets the job done
zz <- ifelse(zz <= 4, "A", "B")

#Returns 126 rows as indicated in comments
unique(zz)

I think this is what you're after. @bill was on the ball with the recommendation of combining unique and combn. We'll also use the apply family to generate ALL of the combinations. Since unique removes duplicate rows, we need to transpose the results from combn before uniqueing them. We then transpose them back before returning to the screen so that each column represents a unique answer.

#Daters
x <- c(rep("A", 4), rep("B",5))
#Generates a list with ALL of the combinations
zz <- sapply(seq_along(x), function(y) combn(x,y))
#Filter out all the duplicates
sapply(zz, function(z) t(unique(t(z))))

Which returns:

[[1]]
     [,1] [,2]
[1,] "A"  "B" 

[[2]]
     [,1] [,2] [,3]
[1,] "A"  "A"  "B" 
[2,] "A"  "B"  "B" 

[[3]]
     [,1] [,2] [,3] [,4]
[1,] "A"  "A"  "A"  "B" 
[2,] "A"  "A"  "B"  "B" 
[3,] "A"  "B"  "B"  "B" 

...

EDIT Since the question is about permuations and not combinations, the answer above is not that useful. This post outlines a function to generate the unique permutations given a set of parameters. I have no idea if it could be improved upon, but here's one approach using that function:

fn_perm_list <-
 function (n, r, v = 1:n)
 {
    if (r == 1)
       matrix(v, n, 1)
    else if (n == 1)
       matrix(v, 1, r)
    else {
       X <- NULL
       for (i in 1:n) X <- rbind(X, cbind(v[i], fn_perm_list(n -
            1, r - 1, v[-i])))
        X
    }
 } 

zz <- fn_perm_list(9, 9)

#Turn into character matrix. This currently does not generalize well, but gets the job done
zz <- ifelse(zz <= 4, "A", "B")

#Returns 126 rows as indicated in comments
unique(zz)
揽清风入怀 2024-11-17 07:01:49

无需生成排列,然后挑选出唯一的排列。
这里有一个更简单的方法(而且也快得多):要生成 4 个 A 和 5 个 B 的所有排列,我们只需要枚举将 4 个 A 放置在 9 个可能位置之间的所有可能方法。这只是一个组合问题。我们可以这样做:

x <- rep('B',9) # vector of 9 B's

a_pos <- combn(9,4) # all possible ways to place 4 A's among 9 positions

perms <- apply(a_pos, 2, function(p) replace(x,p,'A')) # all desired permutations

9x126 矩阵 perms 的每一列都是 4 个 A 和 5 个 B 的唯一排列:

> dim(perms)
[1]   9 126
> perms[,1:4] ## look at first few columns
      [,1] [,2] [,3] [,4]
 [1,] "A"  "A"  "A"  "A" 
 [2,] "A"  "A"  "A"  "A" 
 [3,] "A"  "A"  "A"  "A" 
 [4,] "A"  "B"  "B"  "B" 
 [5,] "B"  "A"  "B"  "B" 
 [6,] "B"  "B"  "A"  "B" 
 [7,] "B"  "B"  "B"  "A" 
 [8,] "B"  "B"  "B"  "B" 
 [9,] "B"  "B"  "B"  "B" 

There's no need to generate permutations and then pick out the unique ones.
Here's a much simpler way (and much, much faster as well): To generate all permutations of 4 A's and 5 B's, we just need to enumerate all possible ways of placing 4 A's among 9 possible locations. This is simply a combinations problem. Here's how we can do this:

x <- rep('B',9) # vector of 9 B's

a_pos <- combn(9,4) # all possible ways to place 4 A's among 9 positions

perms <- apply(a_pos, 2, function(p) replace(x,p,'A')) # all desired permutations

Each column of the 9x126 matrix perms is a unique permutation 4 A's and 5 B's:

> dim(perms)
[1]   9 126
> perms[,1:4] ## look at first few columns
      [,1] [,2] [,3] [,4]
 [1,] "A"  "A"  "A"  "A" 
 [2,] "A"  "A"  "A"  "A" 
 [3,] "A"  "A"  "A"  "A" 
 [4,] "A"  "B"  "B"  "B" 
 [5,] "B"  "A"  "B"  "B" 
 [6,] "B"  "B"  "A"  "B" 
 [7,] "B"  "B"  "B"  "A" 
 [8,] "B"  "B"  "B"  "B" 
 [9,] "B"  "B"  "B"  "B" 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文