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サンプルを選択できますか? たとえば、とは有効なサンプルですが、そうではありません。allrefSample"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 に答える