[R] Sample without replacement

2004-09-22 Thread Mark G Orr
Hello, I have a simple problem (checked the archives and the appropriate 
help pages, to no avail).  I want to creat a vector that is length(2000). 
The vector is to consist of two strings( std and dev) with 1760 stds 
and 240 devs.  Furthermore, for each element of the vector, i want the 
selection of one of the strings to be approx. random.  The end result will 
be a vector with the following proportions:  .12 dev and .88 std. 
My solution, below, almost works, but i don't get the exact desired 
proportions:

sample.from.vector - c(rep(std,1760),rep(dev,240))
make.vector - rep(99,2000)
for (i in 1:2000){
  make.vector[i] - sample(sample.from.vector,1,replace=F)
}
As I understand the above code (which is not very well, obviously), each 
iteration assigns one element from the sample.from.vector  to the current 
make.vector element;  the element choosen from sample.from.vector is 
tagged so that i is not selected again.  However, after the loop, 
make.vector contains, for example, 1758 stds  and 242 devs.  Each 
iteration results in a different proportion of stds and devs (although 
close to the desired, not right on).

Any help would be greatly apprecitated.
-Mark Orr



Mark G. Orr
Postdoctoral Research Fellow
Dept. of Neuroscience
RM 825 Kennedy Center
Albert Einstein College of Medicine
Bronx, NY  10461
718-430-2610
__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] Sample without replacement

2004-09-22 Thread Peter Dalgaard
Mark G Orr [EMAIL PROTECTED] writes:

 Hello, I have a simple problem (checked the archives and the
 appropriate help pages, to no avail).  I want to creat a vector that
 is length(2000). The vector is to consist of two strings( std and
 dev) with 1760 stds and 240 devs.  Furthermore, for each element
 of the vector, i want the selection of one of the strings to be
 approx. random.  The end result will be a vector with the following
 proportions:  .12 dev and .88 std. My solution, below, almost
 works, but i don't get the exact desired proportions:
 
 
 sample.from.vector - c(rep(std,1760),rep(dev,240))
 
 make.vector - rep(99,2000)
 
 for (i in 1:2000){
make.vector[i] - sample(sample.from.vector,1,replace=F)
 }
 
 
 As I understand the above code (which is not very well, obviously),
 each iteration assigns one element from the sample.from.vector  to the
 current make.vector element;  the element choosen from
 sample.from.vector is tagged so that i is not selected again.
 However, after the loop, make.vector contains, for example, 1758 stds
 and 242 devs.  Each iteration results in a different proportion of
 stds and devs (although close to the desired, not right on).
 
 Any help would be greatly apprecitated.

It's staring you right in the face:

make.vector - sample(sample.from.vector)

-- 
   O__   Peter Dalgaard Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics 2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark  Ph: (+45) 35327918
~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907

__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html