[R] random sampling with levels and with replacement

Andreas Borg andreas.borg at unimedizin-mainz.de
Fri Apr 8 11:13:01 CEST 2011


I am not perfectly sure what you want to do, but here is what I would do 
to maintain good/bad ratio in the sample (as Daniel posted, split the 
data and sample from the groups):

df <- data.frame(V1 = 1:400, V2 = c(rep("good",360), rep("bad",40)))
isGood <- which(df$V2=="good")
isBad <- which(df$V2=="bad")
sampleGood <- df[sample(isGood, replace=TRUE),]
sampleBad <- df[sample(isBad, replace=TRUE),]
summary(rbind(sampleGood, sampleBad))

Please include a more specific example with test data (for "final" in 
this case) next time.

Best regards,


taby gathoni schrieb:
> Dear all,
> i have a dataset of about 400 records , with a variable that has  two levels 40 bad and 360 good among other variables,how do i come up  with10 random samples that have the composition of as the main sample  but maintaining the 40 bad 360 good with replacement, i recently discovered that my random samples generated dont maintain the ratio. My code is as  :
> mysample <- final[sample(1:nrow(final), 400,replace=TRUE),] 
> does not give me the ratio of 40 bad and 360 good can anyone give me some pointers please?
> Thanks,
> Taby
> 	[[alternative HTML version deleted]]
> ------------------------------------------------------------------------
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Andreas Borg
Medizinische Informatik

der Johannes Gutenberg-Universität
Institut für Medizinische Biometrie, Epidemiologie und Informatik
Obere Zahlbacher Straße 69, 55131 Mainz

Telefon +49 (0) 6131 175062
E-Mail: borg at imbei.uni-mainz.de

Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der
richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den
Absender und löschen Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe
dieser Mail und der darin enthaltenen Informationen ist nicht gestattet.

More information about the R-help mailing list