【发布时间】:2021-07-02 02:48:45
【问题描述】:
我有两列数据;身份证和专业。每个 ID 都有两次出现,并且出现了唯一的专业。我想从每个专业组中随机抽取 400 个数据样本,其中出现 2 次 ID。我已经尝试使用 sample_n 进行 dplyr 组,但它会以不同的出现次数转换 ID。
数据示例
specialty <- c("obs", "obs", "ped", "ped", "im", "im", "obs", "obs")
ID <- c("M", "M", "K", "K", "l", "l", "N", "N")
shift <- c("night", "day", "night", "day", "night", "day", "night", "day")
如果我对每个专业抽取 2 个样本,我想要的输出是
specialty <- c("obs", "obs", "im", "im", "ped", "ped")
ID <- c("M", "M", "l", "l", "K", "K")
shift <- c("night", "day", "night", "day", "night","day")
我得到的是
specialty <- c("obs", "obs", "im", "im", "ped", "ped")
ID <- c("M", "M", "l", "k", "k", "N")
shift <- c("night", "day", "night", "night", "day", "day")
【问题讨论】:
-
除了编辑问题之外,您是否可以在答案部分中将下面的答案与您使用的代码一起包含在内?将来可能会对其他人有所帮助。