Brad
Brad

Reputation: 680

Sampling on conditions within another column

How can I randomly sample 2 reports that have weights between 0.5 - 1.0

Also; How can I randomly sample 20% of reports that have weights between 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))

Upvotes: 0

Views: 40

Answers (2)

adjustedR2
adjustedR2

Reputation: 161

You can try this :

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)))), ]

Upvotes: 1

neilfws
neilfws

Reputation: 33782

Using dplyr:

library(dplyr)

Sample 2 reports:

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

Sample 20% of reports:

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

Upvotes: 1

Related Questions