[R] Take random sample from class variable

Liaw, Andy andy_liaw at merck.com
Tue Aug 8 18:42:45 CEST 2006


There may be better ways, but this should work:

R> p.yes <- 0.7
R> n.yes <- rbinom(1, nof.sample, p.yes)
R> n.no <- nof.sample - n.yes
R> dat.yes <- mydat[sample(which(mydat$Class == "yes"), n.yes,
replace=TRUE),]
R> dat.no <- mydat[sample(which(mydat$Class == "no"), n.no, replace=TRUE),]

You can rbind() them, and shuffle the rows if you wish.

Andy  

From: Muhammad Subianto
> 
> Dear all,
> Suppose I have a dataset like below, then I take for example, 
> 100 random sample "class" variable where contains "yes" and "no"
> respectively, 70% and 30%.
> I need a new 100 random sample from mydat dataset, but I 
> can't get the result.
> Thanks you very much for any helps.
> Best, Muhammad Subianto
> 
> mydat <- data.frame(size=c(30,12,15,10,12,12,25,30,20,14),
>                        A=c(0,1,0,1,0,1,1,1,0,0),
>                        B=c(1,1,0,1,0,1,1,0,0,1),
>                        C=c(0,0,1,1,0,0,1,1,0,0),
>                        D=c(1,1,1,1,0,1,0,0,1,1),
>                        E=c(1,1,0,1,1,1,1,1,1,0),
> 
> Class=c("yes","yes","no","yes","yes","no","yes","no","yes","yes"))
> mydat
> # Maximal data from dataset
> max.size <- sum(mydat$size);max.size
> # I need sample random
> nof.sample <- 100
> set.seed(123)
> sample.class <- sample(c("yes","no"), nof.sample, prob=c(.7, 
> .3), replace=TRUE) sample.class sampledat.class <- 
> mydat[sample.class,] sampledat.class
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
> 
>



More information about the R-help mailing list