[R] randomizing groups

David Winsemius dwinsemius at comcast.net
Sat Oct 3 01:46:29 CEST 2009


On Oct 2, 2009, at 4:36 PM, Jason Priem wrote:

> I have a list of participants in a study, identified by number.  I  
> want to randomly sort them into an arbitrary number of groups.  
> split(sample(1:96, 96), 1:16)
> almost does it, but it only works where the division is even.  Any  
> ideas?
> Thanks!
>
See if this gives you some ideas:

 > idx <- sample(1:96, 96)
 > cut(idx,  breaks=quantile(idx, prob=c(0, 0.25, 0.5, 0.75, 1),  
include.lowest=TRUE))
  [1] (48.5,72.2] (24.8,48.5] (24.8,48.5] (48.5,72.2] (1,24.8]     
(24.8,48.5] (24.8,48.5] (24.8,48.5] (48.5,72.2]
[10] (48.5,72.2] (1,24.8]    (48.5,72.2] (72.2,96]   (1,24.8]     
(1,24.8]    (24.8,48.5] (1,24.8]    (1,24.8]
[19] (72.2,96]   (72.2,96]   (24.8,48.5] (1,24.8]    (24.8,48.5]  
(24.8,48.5] (24.8,48.5] (72.2,96]   (72.2,96]
[28] (24.8,48.5] (48.5,72.2] (1,24.8]    (48.5,72.2] (48.5,72.2]  
(72.2,96]   (72.2,96]   (1,24.8]    (48.5,72.2]
[37] (48.5,72.2] (48.5,72.2] (24.8,48.5] (48.5,72.2] (1,24.8]     
(1,24.8]    (1,24.8]    (24.8,48.5] (1,24.8]
[46] (24.8,48.5] (72.2,96]   (24.8,48.5] (24.8,48.5] (1,24.8]     
(1,24.8]    (1,24.8]    (1,24.8]    (72.2,96]
[55] (48.5,72.2] (72.2,96]   (1,24.8]    (48.5,72.2] (48.5,72.2]  
(72.2,96]   (48.5,72.2] (48.5,72.2] (24.8,48.5]
[64] (72.2,96]   (48.5,72.2] (1,24.8]    (48.5,72.2] (24.8,48.5]  
(72.2,96]   (48.5,72.2] <NA>        (48.5,72.2]
[73] (48.5,72.2] (1,24.8]    (24.8,48.5] (72.2,96]   (1,24.8]     
(24.8,48.5] (24.8,48.5] (1,24.8]    (72.2,96]
[82] (24.8,48.5] (24.8,48.5] (1,24.8]    (72.2,96]   (48.5,72.2]  
(72.2,96]   (72.2,96]   (72.2,96]   (24.8,48.5]
[91] (72.2,96]   (72.2,96]   (48.5,72.2] (72.2,96]   (72.2,96]    
(72.2,96]
Levels: (1,24.8] (24.8,48.5] (48.5,72.2] (72.2,96]
 > idx.grp <-as.numeric(cut(idx,  breaks=quantile(idx, prob=c(0, 0.25,  
0.5, 0.75, 1), include.lowest=TRUE)))
 > tapply(idx, idx.grp, I)
$`1`
  [1]  6 21  4 14  8  7 20 23 18 22 24 13 17 10  3  2  5 11 19 16  9  
12 15

$`2`
  [1] 38 46 33 34 40 44 27 29 39 28 42 26 47 35 31 45 32 36 37 48 43  
30 41 25

$`3`
  [1] 61 62 70 50 67 69 52 55 51 68 56 54 57 60 53 59 65 66 49 63 72  
64 58 71

$`4`
  [1] 89 86 77 94 93 82 81 80 90 91 76 78 73 88 85 75 96 74 79 95 87  
83 84 92


-- 
David
> Jason Priem,
> Doctoral Student,
> School of Information and Library Science
> University of North Carolina-Chapel Hill
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list