R - 将矢量分成三组,最小尺寸,顺序很重要

时间:2016-02-04 04:01:10

标签: permutation combinations minimum

大家好,并提前感谢您的时间。

我正在寻找可以将矢量分成三组(高,中,低)的不同组合,每组需要包含至少三个数字,它们应该被排序,我想要所有不同的方式这是可能的。

例如;一个1:10的seq,通过眼睛,我有三种方法可以做到这一点

splitting a seq of 1:10 min size of 3

我想要的任何形式的输出都是这样的;

These are the combinations splits with min 3

再次感谢您的时间,如果我混淆排列,我会提前道歉。

1 个答案:

答案 0 :(得分:0)

因此,在对抗这一段时间之后,这就是我所想出的。它产生"低"的最大值。 " high"的最小值,我将定义" medium"通过那些中间的什么。

再次感谢您的时间! _Tim

min_perm <- function(d) { # this function defines all the perms that have min 3 obs in each group
require(gtools) #for combinations

tt = combinations(3, d, repeats.allowed=T) #generate all the perms for three groups

dd = NULL # create an empty vector to hold perms that have at least three in each group
for (i in 1:dim(tt)[1]){
if(length(which(tt[i,] == 1))>=3 & 
     length(which(tt[i,] == 2))>=3 & 
     length(which(tt[i,] == 3))>=3){
  dd = rbind(dd,tt[i,])
  }}

combos = NULL # only need to max of the lowest and min of the highest group, the rest is medium group
for (i in 1:dim(dd)[1]){
combos = rbind(combos,c(max(which(dd[i,] ==1)),min(which(dd[i,] ==3))))}
colnames(combos) = c("low_max","high_min")
return(combos)
}