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

Shuhua Zhan szhan at uoguelph.ca
Thu Oct 30 21:44:28 CET 2008

Hello R users,
I have gene expression data of two groups of genes (large and small). Gene expression intensities of those genes are classified into 1 to 10 levels. What I want is to make a random set of genes that have the same levels as the small group from large group using sample().

I used smallvec to hold the number of genes in each levels (1 to 10) for small group, largevec for large group. I ordered the gene expression data frame of large group (largedf) by the levels and randomly chose the genes with same level as small group. Using the code below I can get the random set of genes from lagre group with same levels for small group. But I got the same set of genes when I run the code in every other runs in Linux. This gives me a doubt in my result.

smallvec<-c(8,12,9,6,13,20,16,11,8,5) ## the No. of genes in levels 1 to 10
generdm<-c() ## a random set of genes
for( i in 1:length(smallvec)){
    generdm<-c(generdm,sample(rownames(largedf)[sum(largevec[0:(i-1)],1):sum(largevec[0:i])],smallvec[i])) ## rownames(largedf) gives gene names ordered by levels in large group

Could you please help me out?
Thanks a lot!!

More information about the R-help mailing list