Re: [R] Take random sample from class variable

From: Muhammad Subianto <msubianto_at_gmail.com>
Date: Wed 09 Aug 2006 - 19:17:30 EST

Dear all,
Andy, thanks you for your help and suggestions. This is exactly what I was looking for.

Kindly regards, Muhammad Subianto

On 8/8/06, Liaw, Andy <andy_liaw@merck.com> wrote:
> 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@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.
> >
> >
>
>
> ------------------------------------------------------------------------------
> Notice: This e-mail message, together with any attachment...{{dropped}}



R-help@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. Received on Wed Aug 09 19:22:22 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 09 Aug 2006 - 20:22:52 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.