0

I have a vector of size 100 (from 1:100).

V <- 1:100

I want to split this vector into 'n' chunks and each chunk will have randomly sampled elements drawn from vector 'V' without replacement. For ex: if n=9, first 8 chunks will have 12 elements each (randomly sampled) and the 9th chunk will have remaining elements i.e. 4

How can I do this ?

Champ
  • 123
  • 6

1 Answers1

1

You can just sample the entire vector, and then use split to generate a list with your desired vectors. For size 12, you can do as below, and just change the denominator as necessary for different sizes.

set.seed(1)
s <- sample(V)
split(s, ceiling(seq_along(s)/12))
#> $`1`
#>  [1] 68 39  1 34 87 43 14 82 59 51 85 21
#> 
#> $`2`
#>  [1] 54 74  7 73 79 37 83 97 44 84 33 35
#> 
#> $`3`
#>  [1] 70 96 42 38 20 28 72 80 40 69 25 99
#> 
#> $`4`
#>  [1] 91 75  6 24 32 94  2 45 18 22 92 90
#> 
#> $`5`
#>  [1]  98  64 100  62  23  67  49  50  65  11  17  36
#> 
#> $`6`
#>  [1] 13 66 47 48 76 29 57 55 77 71 12 16
#> 
#> $`7`
#>  [1] 52 81 89 46 63  9 86 19 56 60 95 10
#> 
#> $`8`
#>  [1] 26 15 78 30  3 58 61 31 27  8 41 53
#> 
#> $`9`
#> [1] 93  5 88  4
caldwellst
  • 5,719
  • 6
  • 22