[R] generate 3 distinct random samples without replacement

Jonathan P Daily jdaily at usgs.gov
Mon Mar 7 21:18:21 CET 2011


would this work?

s <- sample(d1, 300, F)
D <- data.frame(a = s[1:100], b = s[101:200], c = s[201:300])
--------------------------------------
Jonathan P. Daily
Technician - USGS Leetown Science Center
11649 Leetown Road
Kearneysville WV, 25430
(304) 724-4480
"Is the room still a room when its empty? Does the room,
 the thing itself have purpose? Or do we, what's the word... imbue it."
     - Jubal Early, Firefly

r-help-bounces at r-project.org wrote on 03/07/2011 02:17:19 PM:

> [image removed] 
> 
> [R] generate 3 distinct random samples without replacement
> 
> Cesar Hincapié 
> 
> to:
> 
> r-help
> 
> 03/07/2011 03:06 PM
> 
> Sent by:
> 
> r-help-bounces at r-project.org
> 
> Hello:
> 
> I wonder if I could get a little help with random sampling in R.
> 
> I have a vector of length 7375.  I would like to draw 3 distinct 
> random samples, each of length 100 without replacement.  I have 
> tried the following:
> 
> d1 <- 1:7375
> 
> set.seed(7)
> i <- sample(d1, 100, replace=F)
> s1 <- sort(d1[i])
> s1
> 
> d2 <- d1[-i]
> set.seed(77)
> j <- sample(d2, 100, replace=F)
> s2 <- sort(d2[j])
> s2
> 
> d3 <- d2[-j]
> set.seed(777)
> k <- sample(d3, 100, replace=F)
> s3 <- sort(d3[k])
> s3
> 
> D <- data.frame(a=s1,b=s2,c=s3)
> 
> 
> However, s2 is only 97 elements long, and s3, only 96 long.
> 
> I would appreciate any suggestions on a better approach.
> I'm also curious to know why my second and third samples are less 
> than 100 elements in length.
> 
> Thanks for your time and consideration,
> 
> Cesar A. Hincapié, DC, MHSc
> 
> Research Fellow, Division of Health Care and Outcomes Research, 
> Toronto Western Research Institute
> PhD Candidate in Epidemiology, Dalla Lana School of Public Health, 
> University of Toronto
> e. cesar.hincapie at utoronto.ca
> 
> 
> 
> 
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> 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