【发布时间】:2018-12-24 14:09:31
【问题描述】:
假设我有一个带有重复元素的简单向量:
a <- c(1,1,1,2,2,3,3,3)
有没有办法从每个重复的元素中随机选择一个独特的元素? IE。一个随机抽签指出要保留的元素是:
1,4,6 ## here I selected the first 1, the first 2 and the first 3
还有一个:
1,5,8 ## here I selected the first 1, the second 2 and the third 3
我可以为每个重复的元素循环执行此操作,但我确信必须有更快的方法来执行此操作?
编辑:
理想情况下,如果某个元素已经是唯一元素,则解决方案也应该始终选择该元素。 IE。我的向量也可以是:
b <- c(1,1,1,2,2,3,3,3,4) ## The number four is unique and should always be drawn
【问题讨论】: