[R] Random Sampling from a Matrix

Uwe Ligges ligges at statistik.uni-dortmund.de
Wed Aug 22 10:05:54 CEST 2007



Anup Nandialath wrote:
> Dear Friends,
> 
> I have a matrix of size 5000 X 20. The first two columns are indicator variables taking the value of either 0 or 1. Let us call the first two columns Y1 and Y2. 
> 
> I need to randomly sample 1000 rows with all the associated columns, in other words my new matrix should be of size 1000 X 20. I realize that using this command
> 
>  newmat <- mainmat[sample(1000,replace=F),]
> 
> achieves this. However, I would like to make sure that both Y1 and Y2 have more or less an equal amount of 0's and 1's. At present when I sample, I get cases where sometimes all my Y2's are 0. Is there any way to accomodate this problem.


Yes, for example drawing samples stratified by the values of Y1 or Y2.

Uwe Ligges



> Thanks in advance.
> 
> Regards
> 
> Anup
> 
>        
> ---------------------------------
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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