【问题标题】:R - splitting a vector into three groups, min size, order mattersR - 将向量分成三组,最小大小,顺序很重要
【发布时间】:2016-02-10 05:50:36
【问题描述】:

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

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

例如; 1:10 的序列,我用肉眼数出了三种方法

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

再次感谢您抽出宝贵时间,如果我将组合与排列混淆了,我提前道歉。

提姆

【问题讨论】:

    标签: permutation combinations minimum


    【解决方案1】:

    所以在我的头撞了一会儿之后,这就是我想出的。它产生“低”的最大值和“高”的最小值,我将通过它们中间的内容来定义“中”。

    再次感谢您的宝贵时间!! _蒂姆

    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)
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-02
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-19
      • 1970-01-01
      相关资源
      最近更新 更多