2

类似于我在Using a sample list as a template for sampling from a large list without wraparound 的问题,我怎么知道这样做是否允许环绕?

因此,如果我有一个字母向量:

> all <- letters
> all
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"

然后我从字母中定义一个参考样本,如下所示:

> refSample <- c("j","l","m","s")

其中元素之间的间距为 2(第 1 到第 2)、1(第 2 到第 3)和 6(第 3 到第 4),然后我如何从中选择n 个样本all,其元素之间具有相同的环绕间距refSample?例如"a","c","d","j""q" "s" "t" "z""r" "t" "u" "a"将是有效样本,但"a","c","d","k"不会。

同样,为函数参数化是最好的。

4

1 回答 1

3

我本来可以把它当作练习,但这里有——

all <- letters                                                                                                                                                                                                                                                                
refSample <- c("j","l","m","s")    

pick_matches <- function(n, ref, full, wrap = FALSE) {                                                                                                                                                                                                                        
  iref <- match(ref,full)                                                                                                                                                                                                                                                     
  spaces <- diff(iref)                                                                                                                                                                                                                                                        
  tot_space <- sum(spaces)                                                                                                                                                                                                                                                    
  N <- length( full ) - 1                                                                                                                                                                                                                                                     
  max_start <- N  - tot_space*(1-wrap)                                                                                                                                                                                                                                        
  starts <- sample(0:max_start, n, replace = TRUE)                                                                                                                                                                                                                            
  return( sapply( starts, function(s) full[ 1 + cumsum(c(s, spaces)) %% (N+1)  ] ) )                                                                                                                                                                                          
}                                                                                                                                                                                                                                                                             


> set.seed(1)                                                                                                                                                                                                                                                                 



> pick_matches(5, refSample, all, wrap = FALSE)                                                                                                                                                                                                                              
      [,1] [,2] [,3] [,4] [,5]                                                                                                                                                                                                                                                
 [1,] "e"  "g"  "j"  "p"  "d"                                                                                                                                                                                                                                                 
 [2,] "g"  "i"  "l"  "r"  "f"                                                                                                                                                                                                                                                 
 [3,] "h"  "j"  "m"  "s"  "g"                                                                                                                                                                                                                                                 
 [4,] "n"  "p"  "s"  "y"  "m"          

> pick_matches(5, refSample, all, wrap = TRUE)                                                                                                                                                                                                                               
      [,1] [,2] [,3] [,4] [,5]                                                                                                                                                                                                                                                
 [1,] "x"  "y"  "r"  "q"  "b"                                                                                                                                                                                                                                                 
 [2,] "z"  "a"  "t"  "s"  "d"                                                                                                                                                                                                                                                 
 [3,] "a"  "b"  "u"  "t"  "e"                                                                                                                                                                                                                                                 
 [4,] "g"  "h"  "a"  "z"  "k"           
于 2012-05-04T01:36:40.433 回答