Pregunta

Possible Duplicate:
Random sampling a list of genes

I would like to create 1000 random lists of 1652 genes from a universe of 19.000 genes. I decided to replace since the universe is not so big. The only condition is that lists can contain similar genes between them (due to replacement) but each list can not contain a gene more than one time. So it will be unique in the single list. Any suggestion about this?

Ex: Universe = letters[1:26]

Desired output:

 [[1]]  [[2]]   [[3]]   [[...]]
   a      b       f
   b      c       a
   c      d       b
   f      z       j
   h      j       o

I would like avoid a situation like:

 [[1]]  [[...]]
  a   
  a   
  b   
  c
  c

Since the universe is not so big I cannot set REPLACE = F. If I set REPLACE = T, replicated elements occur in the list...this is what I'm trying to avoid for my analysis.

Thanks in advance

E.

¿Fue útil?

Solución

This code draws 5 samples of 10 from the Universe, without replacement. I think this is what you want:

Universe = letters[1:26]
replicate(5, sample(Universe, 10, replace = FALSE))

     [,1] [,2] [,3] [,4] [,5]
 [1,] "j"  "l"  "k"  "c"  "j" 
 [2,] "g"  "i"  "c"  "t"  "g" 
 [3,] "z"  "u"  "m"  "u"  "e" 
 [4,] "a"  "b"  "t"  "e"  "q" 
 [5,] "q"  "d"  "j"  "k"  "m" 
 [6,] "r"  "a"  "l"  "l"  "x" 
 [7,] "e"  "g"  "r"  "i"  "f" 
 [8,] "l"  "w"  "o"  "g"  "u" 
 [9,] "b"  "y"  "b"  "x"  "c" 
[10,] "u"  "j"  "x"  "a"  "b" 

Otros consejos

Not sure what you mean by "REPLACE = T" but random.sample might do what you want

>>> import random
>>> import string
>>> universe = string.ascii_lowercase
>>> random.sample(universe, 5)
['z', 'n', 'p', 'u', 's']

using numbers as the universe

>>> universe = range(19000)
>>> result = [random.sample(universe, 1652) for x in range(1000)]

takes less than a second to run. If you want to avoid duplicates (unlikely in the first place) you can use a set

>>> result = set()
>>> while len(result) < 1000:
...     result.add(tuple(random.sample(universe, 1652)))
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top