[R] why does sample(x, n) give the same n items in every separate runs?

Dieter Menne dieter.menne at menne-biomed.de
Thu Oct 30 22:12:11 CET 2008


Shuhua Zhan <szhan <at> uoguelph.ca> writes:

> 
> smallvec<-c(8,12,9,6,13,20,16,11,8,5)  
> largevec<-c(400,300,550,600,210,420,380,600,450,500)
> generdm<-c() ## a random set of genes
> for( i in 1:length(smallvec)){
> 
# try to chop down this line and add a print to understand
# what is going on
generdm<-c(generdm,
  sample(rownames(largedf)[sum(largevec[0:i-)],1):sum(largevec[0:i])],smallvec[i]))

> ## rownames(largedf) gives gene names ordered by levels in 
> }

Since your code does not run, and you do not show how you got your 
result, it is a bit of guesswork. I believe it has nothing to do with 
sample(), but rather with the way you "store" (=not store) the results. 
Your way of storing looks a bit like PHP.

Try a variation of this one

generdm = list()
for (i in 1:10)
{
  generdm[[i]] = rnorm(10) # put your sample construct here
}
generdm

Dieter



More information about the R-help mailing list