使用样本列表作为模板,从包含环绕的更大列表中进行采样

时间:2012-05-04 00:30:43

标签: r sampling

Using a sample list as a template for sampling from a larger 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),我怎样才能从{{1}中选择 n 样本它的元素与all之间有相同的环绕间距?例如,refSample"a","c","d","j""q" "s" "t" "z"将是有效样本,但"r" "t" "u" "a"则不会。

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

1 个答案:

答案 0 :(得分: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"