r语言 - 使用示例列表作为模板,从具有环绕功能的较大列表中进行采样

  • 本文关键字:列表 功能 采样 r语言 r sampling
  • 更新时间 :
  • 英文 :


类似于我的问题,即使用示例列表作为模板,以便在没有环绕的情况下从较大的列表中进行采样,我怎么知道这样做允许环绕?

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

> 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),那么我如何从元素之间具有相同环绕间距的all中选择 n 个样本以refSample?例如,"a","c","d","j""q" "s" "t" "z""r" "t" "u" "a"将是有效的样本,但"a","c","d","k"则不是。

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

我会把它作为一个练习,但这里是——

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"           

最新更新