洗牌向量 - sample() 的所有可能结果? [英] Shuffling a vector - all possible outcomes of sample()?
问题描述
我有一个包含五个项目的向量.
I have a vector with five items.
my_vec <- c("a","b","a","c","d")
如果我想将这些值重新排列成一个新的向量(shuffle),我可以使用 sample():
If I want to re-arrange those values into a new vector (shuffle), I could use sample():
shuffled_vec <- sample(my_vec)
简单 - 但 sample() 函数只给了我一种可能的洗牌.如果我想知道所有可能的改组组合怎么办?各种combn"函数似乎没有帮助,expand.grid() 给了我所有可能的组合 with 替换,当我需要它时没有替换.执行此操作的最有效方法是什么?
Easy - but the sample() function only gives me one possible shuffle. What if I want to know all possible shuffling combinations? The various "combn" functions don't seem to help, and expand.grid() gives me every possible combination with replacement, when I need it without replacement. What's the most efficient way to do this?
请注意,在我的向量中,我有两次值a" - 因此,在返回的混洗向量集合中,它们都应该在集合中包含两次a".
Note that in my vector, I have the value "a" twice - therefore, in the set of shuffled vectors returned, they all should each have "a" twice in the set.
推荐答案
我认为来自组合包的 permn
可以满足您的需求
I think permn
from the combinat package does what you want
library(combinat)
permn(my_vec)
一个较小的例子
> x
[1] "a" "a" "b"
> permn(x)
[[1]]
[1] "a" "a" "b"
[[2]]
[1] "a" "b" "a"
[[3]]
[1] "b" "a" "a"
[[4]]
[1] "b" "a" "a"
[[5]]
[1] "a" "b" "a"
[[6]]
[1] "a" "a" "b"
如果重复是一个问题,你可以做类似的事情来摆脱重复
If the duplicates are a problem you could do something similar to this to get rid of duplicates
strsplit(unique(sapply(permn(my_vec), paste, collapse = ",")), ",")
或者可能是删除重复项的更好方法...
Or probably a better approach to removing duplicates...
dat <- do.call(rbind, permn(my_vec))
dat[duplicated(dat),]
这篇关于洗牌向量 - sample() 的所有可能结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!