R-查找值的唯一排列 [英] R- Find Unique Permutations of Values
问题描述
我希望创建一个包含两个不同值的向量的所有可能排列,其中我控制每个值的比例.
例如,如果我有一个长度为3的向量,并且我希望所有可能的组合都包含一个单一的1,那么我想要的输出就是一个看起来像这样的列表:
list.1 <- list(c(1,0,0), c(0,1,0), c(0,0,1))
相反,如果我希望所有可能的组合包含三个1,那么我想要的输出是一个看起来像这样的列表:
list.3 <- list(c(1,1,1))
换句话说,1
和0
值的模式很重要,但是所有1
都应被视为与所有其他1
相同.
基于此处和其他位置的搜索,我尝试了几种方法:
expand.grid(0:1, 0:1, 0:1) # this includes all possible combinations of 1, 2, or 3 ones
permn(c(0,1,1)) # this does not treat the ones as identical (e.g. it produces (0,1,1) twice)
unique(permn(c(0,1,1))) # this does the job!
因此,使用软件包combinat
中的函数permn
似乎很有希望.但是,在我将其扩展到我的实际问题(长度为20,具有50%1s和50%0s的向量)时,我遇到了问题:
unique(permn(c(rep(1,10), rep(0, 10))))
# returns the error:
Error in vector("list", gamma(n + 1)) :
vector size specified is too large
我的理解是,发生这种情况是因为,在对permn
的调用中,它创建了一个包含所有可能排列的列表,即使其中许多排列是相同的,并且该列表太大,R无法处理.>
有人对如何解决这个问题有建议吗?
很抱歉,如果以前已经回答过-SO问题很多,包含相似的语言,但问题不同,我无法找到满足我需要的解决方案!
expand.grid
包括所有排列不应该成为一个破坏者.只需在以下位置添加一个子集:
combinations <- function(size, choose) {
d <- do.call("expand.grid", rep(list(0:1), size))
d[rowSums(d) == choose,]
}
combinations(size=10, choose=3)
# Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10
# 8 1 1 1 0 0 0 0 0 0 0
# 12 1 1 0 1 0 0 0 0 0 0
# 14 1 0 1 1 0 0 0 0 0 0
# 15 0 1 1 1 0 0 0 0 0 0
# 20 1 1 0 0 1 0 0 0 0 0
# 22 1 0 1 0 1 0 0 0 0 0
...
I am hoping to create all possible permutations of a vector containing two different values, in which I control the proportion of each of the values.
For example, if I have a vector of length three and I want all possible combinations containing a single 1, my desired output is a list looking like this:
list.1 <- list(c(1,0,0), c(0,1,0), c(0,0,1))
In contrast, if I want all possible combinations containing three 1s, my desired output is a list looking like this:
list.3 <- list(c(1,1,1))
To put it another way, the pattern of the 1
and 0
values matter, but all 1
s should be treated as identical to all other 1
s.
Based on searching here and elsewhere, I've tried several approaches:
expand.grid(0:1, 0:1, 0:1) # this includes all possible combinations of 1, 2, or 3 ones
permn(c(0,1,1)) # this does not treat the ones as identical (e.g. it produces (0,1,1) twice)
unique(permn(c(0,1,1))) # this does the job!
So, using the function permn
from the package combinat
seems promising. However, where I scale this up to my actual problem (a vector of length 20, with 50% 1s and 50% 0s, I run into problems:
unique(permn(c(rep(1,10), rep(0, 10))))
# returns the error:
Error in vector("list", gamma(n + 1)) :
vector size specified is too large
My understanding is that this is happening because, in the call to permn
, it makes a list containing all possible permutations, even though many of them are identical, and this list is too large for R to handle.
Does anyone have a suggestion for how to work around this?
Sorry if this has been answered previously - there are many, many SO questions containing similar language but different problems and I have not bene able to find a solution which meets my needs!
It should not be a dealbreaker that expand.grid
includes all permutations. Just add a subset after:
combinations <- function(size, choose) {
d <- do.call("expand.grid", rep(list(0:1), size))
d[rowSums(d) == choose,]
}
combinations(size=10, choose=3)
# Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10
# 8 1 1 1 0 0 0 0 0 0 0
# 12 1 1 0 1 0 0 0 0 0 0
# 14 1 0 1 1 0 0 0 0 0 0
# 15 0 1 1 1 0 0 0 0 0 0
# 20 1 1 0 0 1 0 0 0 0 0
# 22 1 0 1 0 1 0 0 0 0 0
...
这篇关于R-查找值的唯一排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!