-1

I would like to create 1000 random lists of 1652 genes from a universe of 44.400 genes. I decided to replace. I used the following instruction to create the random lists:

randomMatrix<-replicate(1000, sample(gene_list, 1652, replace = T))

The point is that in each list a gene is replicated. For my study, genes can be replicated between lists but not in each list. How can I impose not to replicate genes in each single list?

Thanks in advance

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
NewUsr_stat
  • 2,351
  • 5
  • 28
  • 38

2 Answers2

2

It should work with replace = FALSE:

randomMatrix<-replicate(1000, sample(gene_list, 1652, replace = FALSE))

This, of course, requires at least 1652 unique values in gene_list.

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • By imposition of my boss I have to replace. But what I need is to replace with the condition that in each list no gene have to be replicated. Of course between lists genes are replicated due to replace = TRUE – NewUsr_stat Oct 05 '12 at 15:50
  • @Elb The `sample` command is replicated 1000 times. In each run you obtain unique values if `replace = FALSE`. But this has no effect on the other runs. Hence, between lists you actually replace the "genes". – Sven Hohenstein Oct 05 '12 at 15:55
0

A reproducible example would be nice to illustrate your problem, since you didn't give us such example I just assume a List and made some replications

List <- list(c(2,1,3,4,5,6), c(1,4,5,7,0,6), c(2,4,7,9,3,1))
set.seed(001)
replicate(3, lapply(List, sample, 7, replace=TRUE), simplify = FALSE)

which produces

[[1]]
[[1]][[1]]
[1] 1 3 4 6 1 6 6

[[1]][[2]]
[1] 7 7 1 4 4 0 5

[[1]][[3]]
[1] 3 7 3 1 7 3 1


[[2]]
[[2]][[1]]
[1] 1 4 2 1 3 2 3

[[2]][[2]]
[1] 6 5 5 7 5 4 0

[[2]][[3]]
[1] 3 3 2 3 7 3 9


[[3]]
[[3]][[1]]
[1] 5 4 4 5 2 3 5

[[3]][[2]]
[1] 0 5 6 5 4 1 1

[[3]][[3]]
[1] 4 9 9 7 1 4 7

Note that this approach will give you a resampled data (with replacement) for each element of your original list, that's why each replication is a list consisting in three elements each one.

If you write sapply instead of lapply inside replicate(...) the resulting output would be nicer.

set.seed(001)
replicate(3, sapply(List, sample, 7, replace=TRUE), simplify = FALSE)
[[1]]
     [,1] [,2] [,3]
[1,]    1    7    3
[2,]    3    7    7
[3,]    4    1    3
[4,]    6    4    1
[5,]    1    4    7
[6,]    6    0    3
[7,]    6    5    1

[[2]]
     [,1] [,2] [,3]
[1,]    1    6    3
[2,]    4    5    3
[3,]    2    5    2
[4,]    1    7    3
[5,]    3    5    7
[6,]    2    4    3
[7,]    3    0    9

[[3]]
     [,1] [,2] [,3]
[1,]    5    0    4
[2,]    4    5    9
[3,]    4    6    9
[4,]    5    5    7
[5,]    2    4    1
[6,]    3    1    4
[7,]    5    1    7
Community
  • 1
  • 1
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
  • Hi Jibert. Thank you a lot for your help but probably I don't understand your suggestion. What I would like to avoid is to have for ex. in [[1]] repeated number 6 in [,1], in [[2]] repeated 7 and 4 and so on. – NewUsr_stat Oct 05 '12 at 15:26
  • @Elb so set `replace=FALSE`. It this doesn't work as you expect then give us some data to work with and show us the expected output so that we can fully understand your problem. – Jilber Urbina Oct 05 '12 at 15:33
  • mmmmm...I cannot....my boss told me to sample with replacement according to the data we have.. – NewUsr_stat Oct 05 '12 at 15:37
  • I you are re-sampling with replacement you'll have repeated values. – Jilber Urbina Oct 05 '12 at 15:40
  • Ok repeated genes between lists but not in the same list. – NewUsr_stat Oct 05 '12 at 15:42