0

我有以下

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
   }
}

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

4

3 回答 3

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"
于 2013-07-16T16:16:14.643 回答
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]
于 2013-07-16T16:23:38.823 回答
-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]

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

于 2013-07-16T17:12:38.377 回答