对另一列内的条件进行抽样

问题描述 投票:0回答:2

如何随机抽样2个重量在0.5-1.0之间的报告

也;如何随机抽样20%的权重介于0.5 - 1.0之间的报告

DF <- data.frame(Report_ID=c(2,8,12,15,16, 51,67,89,88,98),
                        Weight=c(0.05,0.1,0.25,0.30,0.35,0.56,0.75,0.81,0.95,1.0))
r random
2个回答
1
投票

使用dplyr

library(dplyr)

样本2报告:

DF %>% 
  filter(between(Weight, 0.5, 1)) %>% 
  sample_n(2)

样本的20%报告:

DF %>% 
  filter(between(Weight, 0.5, 1)) %>% 
  sample_frac(0.5)

1
投票

你可以试试这个:

rand_sample <- DF[ sample( which(DF$Weight > 0.4 & DF$Weight < 1.1), round(0.2*length(which(DF$Weight > 0.4 & DF$Weight < 1.1)))), ]
© www.soinside.com 2019 - 2024. All rights reserved.