随机选择项目列表中的项目

时间:2013-07-16 16:00:21

标签: r random-sample

我有以下

cells = c(3, 5, 8, 6)
names(cells) = c('A', 'B', 'C', 'D')

我想随机选择一半的细胞,然后分配给一个新的载体m1,另一半的细胞将被分配到m2 = cells - m1。我是R的新手,样本函数只允许我选择一种类型的所有单元格,而不是一次选择一个单独的单元格,所以我不确定从这里去哪里。

我的代码目前看起来像这样

y = ceiling(sum(cells)/2)


for(i in 1:y){

   z = sample(cells[cells>0], 1, replacement = FALSE, prob = NULL)

   if(z == cells[1]){
      cells[1] = cells[1] - 1
      m1[1] = m1[1] + 1
   }
   if(z == cells[2]){
      cells[2] = cells[2] - 1
      m1[2] = m1[2] + 1
   }
   if(z == cells[3]){
      cells[3] = cells[3] - 1
      m1[3] = m1[3] + 1
   } 
   if(z == cells[4]){
      cells[4] = cells[4] - 1
      m1[4] = m1[4] + 1
   }
}

我知道这是错误的,因为它只是随机选择单元格类型,而不是随机选择每个单元格。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

为什么不定义cells = c(rep('A',3),rep('B',5), rep('C',8), rep('D',6))然后

    > cells
     [1] "A" "A" "A" "B" "B" "B" "B" "B" "C" "C" "C" "C" "C" "C" "C" "C" "D" "D" "D"
    [20] "D" "D" "D"

    x <- sample(1:length(cells), ceiling(length(cells)/2))

    m1 <- cells[x]
     [1] "D" "B" "C" "A" "B" "A" "B" "D" "D" "C" "C"

    m2 <- cells[setdiff(1:length(cells), x)]
     [1] "A" "B" "B" "C" "C" "C" "C" "C" "D" "D" "D"

答案 1 :(得分:0)

或者你可以在样本函数中使用参数prob

x <- sample(letters[1:4], 22, replace = T, prob = c(3,5,8,6))
x
[1] "d" "c" "a" "c" "b" "a" "b" "b" "b" "c" "b" "c" "d" "c" "c" "b" "c" "b" "c" "d" "c" "b"
m1 <- x[1:11]; m2 <- x[12:22]

答案 2 :(得分:-1)

不是直接从cells采样,而是采用索引并使用它们来选择元素。

cells = c(3, 5, 8, 6)
categories = c('A', 'B', 'C', 'D')

n  = length(cells)
cells_long = unlist(sapply(1:n
                          , function(i){ rep(categories[i],cells[i]) }
                          ))

n  = length(cells_long)
ix = sample(1:n, floor(n/2))
m1 = cells_long[ix]
m2 = cells_long[-ix]

这也是从复杂对象或相关对象集合中采样的有用技巧。