Please note is with replacement From: taby gathoni <tabieg@yahoo.com> To: R help <r-help@r-project.org> Sent: Thursday, March 10, 2011 11:53 AM Subject: [R] random sampling steps in R Dear all, Could someone assist me in random sampling steps/code in R? I have a main sample of 42 males and 165 females and I want to come up with about 1000 samples of 20 males and 20 females from this main sample. While at it, i would also like to come up Accuracy Ratios (ARs) with corresponding confidence intervals. Please assist. Thanks so much, Taby [[alternative HTML version deleted]] ______________________________________________ R-help@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. [[alternative HTML version deleted]]
Hello, Taby, for your (first) problem take a look at ?sample and its argument replace. Regards -- Gerrit On Thu, 10 Mar 2011, taby gathoni wrote:> Please note is with replacement > > > From: taby gathoni <tabieg at yahoo.com> > To: R help <r-help at r-project.org> > Sent: Thursday, March 10, 2011 11:53 AM > Subject: [R] random sampling steps in R > > Dear all, > > Could someone assist me in random sampling steps/code in R? I have a > main sample of 42 males and 165 females and I want to come up with about > 1000 samples of 20 males and 20 females from this main sample. While at > it, i would also like to come up Accuracy Ratios (ARs) with > corresponding confidence intervals. > > Please assist. > > > Thanks so much, > > 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.
andrija djurovic
2011-Mar-10 10:05 UTC
[R] Fw: random sampling steps in R with replacement
Hi Taby, this is not best solution for sure but it will give you, maybe, some ideas :) I am also looking forward to see other solutions. data<-data.frame(id=1:(165+42),x=rep(c("male","female"),c(42,165))) f<-function(x) { str.sample<-list() for (i in 1:length(levels(x$x))) { str.sample[[i]]<-x[x$x==levels(x$x)[i] ,][sample(tapply(x$x,x$x,length)[i],20,rep=T),] } strat.sample<-do.call("rbind",str.sample) return(strat.sample) } f(data) repl<-list() for(i in 1:1000) { repl[[i]]<-f(data) } repl Andrija On Thu, Mar 10, 2011 at 10:04 AM, taby gathoni <tabieg@yahoo.com> wrote:> Please note is with replacement > > > > From: taby gathoni <tabieg@yahoo.com> > To: R help <r-help@r-project.org> > Sent: Thursday, March 10, 2011 11:53 AM > Subject: [R] random sampling steps in R > > Dear all, > > Could someone assist me in random sampling steps/code in R? I have a main > sample of 42 males and 165 females and I want to come up with about 1000 > samples of 20 males and 20 females from this main sample. While at it, i > would also like to come up Accuracy Ratios (ARs) with corresponding > confidence intervals. > > Please assist. > > > Thanks so much, > > Taby > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Hi: To get the samples, here's one approach: df <- data.frame(gender = rep(c('F', 'M'), c(165, 42)), y = rpois(207, 20)) # Sampling function to take 20 F and 20 M with replacement # The sample function operates on the rows of df to get idx and then takes # the y's corresponding to those rows sfun <- function(x) { idx <- c(sample(1:165, 20, replace = TRUE), sample(166:207, 20, replace = TRUE)) x[idx] } # Replicate the process 1000 times u <- t(replicate(1000, sfun(df$y)))> dim(u)[1] 1000 40 The first 20 rows are F, the second 20 M. HTH, Dennis On Thu, Mar 10, 2011 at 1:04 AM, taby gathoni <tabieg@yahoo.com> wrote:> Please note is with replacement > > > > From: taby gathoni <tabieg@yahoo.com> > To: R help <r-help@r-project.org> > Sent: Thursday, March 10, 2011 11:53 AM > Subject: [R] random sampling steps in R > > Dear all, > > Could someone assist me in random sampling steps/code in R? I have a main > sample of 42 males and 165 females and I want to come up with about 1000 > samples of 20 males and 20 females from this main sample. While at it, i > would also like to come up Accuracy Ratios (ARs) with corresponding > confidence intervals. > > Please assist. > > > Thanks so much, > > Taby > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]