R 迭代 select 并从向量中删除值

R iteratively select and remove values from a vector

我想要迭代 select 向量中的一些值(基于抛硬币的决策概率)并将它们从该向量中移除,然后在下一个迭代循环中,我再次想要select(抛硬币后)来自剩余向量值的值。直到我达到我的向量为空的程度。以下是我心中的解决方案,但最后我陷入了向量中的一个非 selected 值:

vector <- c("item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9", "item10")
  for (i in 1:10) {
    #select values from the vector based on coin-toss probability, so that roughly half of the items get selected
  selected <- sample(vector, replace = F, size = length(vector)/2)
  print(slected)
  # Do some operation with slected values

  # remove the selcted values from the original vector
  vector <- vector[!vector%in%selected]
  print(vector)
  # as we are in loop this will keep happening until we are done selecting all of the elements in the vector.
  }

NOTE: I don't want to select any value twice!

任何人都可以指导我什么是更好的解决方案。

编辑:可以有一个基于抛硬币的 selection,我没有明确给出大小。例如,对于向量中的每个值,我们计算 selection 的概率,如果它高于 0.5,则该值得到 selected,否则不会。

我想这样做是因为我想迭代这个向量 1000 次,我希望在每次迭代中根据不同类型的 selection 得到不同的结果。

这里有一个不同的解决方案。请注意,最重要的变化是在定义样本量时使用 ceiling

x <- c("item1", "item2", "item3", "item4", "item5", "item6", "item7",
       "item8", "item9", "item10")

while(length(x) > 0) {
  selected <- sample(x, replace = FALSE, size = ceiling(length(x)/2))
  cat("selected:", selected, "\n")
  x <- x[!x %in% selected]
  cat("remaining:", x, "\n\n")
}

selected: item5 item3 item8 item10 item4 
remaining: item1 item2 item6 item7 item9 

selected: item1 item2 item9 
remaining: item6 item7 

selected: item6 
remaining: item7 

selected: item7 
remaining: 

我还使用了 while 循环而不是 OP 的 for 循环,因为这在概念上更有意义。


关于 OP 的评论:

您也可以尝试类似下面的操作,其中您没有定义所选择的样本大小。但是请注意,这很容易导致某些情况下没有或所有元素都被选中,即使每个元素的概率为 0.5:

x <- c("item1", "item2", "item3", "item4", "item5", "item6", "item7", 
       "item8", "item9", "item10")
while(length(x) > 0) {
  selected <- x[sample(c(TRUE, FALSE), size = length(x), replace = TRUE)]
  cat("selected:", selected, "\n")
  x <- x[!x %in% selected]
  cat("remaining:", x, "\n\n")
}