Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用样本列表作为模板,从具有概括功能的较大列表中进行采样_R_Sampling - Fatal编程技术网

R 使用样本列表作为模板,从具有概括功能的较大列表中进行采样

R 使用样本列表作为模板,从具有概括功能的较大列表中进行采样,r,sampling,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" >全部 [1] “a”“b”“c”“d”“e”“f”“g”“h”“i”“j”“k”“l”“m”“n”“o”“p”“q”“r”“s”“t”“u

类似于我在的问题,我如何知道这样做允许一个环绕

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

> 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"
>全部
[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")

>refSample我本想把它作为一个练习,但现在开始--


你的模运算出了点问题。我建议你定义
mod@flodel——事实上,谢谢你指出——我有N-1而不是N+1;修好了。
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"