Hi there, I want to generate different samples using the followindg code: g<-sample(LETTERS[1:2], 24, replace=T) How can I specify that I need 12 "A"s and 12 "B"s? Thank you, Judith ____________________________________________________________________________________ Be a better friend, newshound, and
sample(rep(LETTERS[1:2],12), 24, replace=F) -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Judith Flores Sent: Tuesday, February 05, 2008 1:52 PM To: RHelp Subject: [R] Sampling Hi there, I want to generate different samples using the followindg code: g<-sample(LETTERS[1:2], 24, replace=T) How can I specify that I need 12 "A"s and 12 "B"s? Thank you, Judith ____________________________________________________________________________ ________ Be a better friend, newshound, and ______________________________________________ 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.
Would this work: g<-sample(rep(LETTERS[1:2],12), 24, replace=F) HTH -Kevin -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Judith Flores Sent: Tuesday, February 05, 2008 1:52 PM To: RHelp Subject: [R] Sampling Hi there, I want to generate different samples using the followindg code: g<-sample(LETTERS[1:2], 24, replace=T) How can I specify that I need 12 "A"s and 12 "B"s? Thank you, Judith ________________________________________________________________________ ____________ Be a better friend, newshound, and ______________________________________________ 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.
On 2/5/2008 1:52 PM, Judith Flores wrote:> Hi there, > > I want to generate different samples using the > followindg code: > > > g<-sample(LETTERS[1:2], 24, replace=T) > > How can I specify that I need 12 "A"s and 12 "B"s? > > Thank you, > > Judithx <- rep(c("A","B"), each=12) x [1] "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" [12] "A" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" [23] "B" "B" # "sample(x) generates a random permutation of the elements of x" g <- sample(x) g [1] "A" "A" "B" "A" "B" "B" "B" "B" "A" "B" "A" [12] "A" "B" "A" "A" "A" "B" "B" "B" "A" "A" "B" [23] "A" "B"> ____________________________________________________________________________________ > Be a better friend, newshound, and > > ______________________________________________ > 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.-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Judith Flores > Sent: Tuesday, February 05, 2008 10:52 AM > To: RHelp > Subject: [R] Sampling > > Hi there, > > I want to generate different samples using the > followindg code: > > > g<-sample(LETTERS[1:2], 24, replace=T) > > How can I specify that I need 12 "A"s and 12 "B"s? > > Thank you, > > Judith > >Judith, Does this do what you want? g<-sample(rep(LETTERS[1:2],12)) Hope this is helpful, Dan Daniel Nordlund Bothell, WA USA
>From: Judith Flores <juryef at yahoo.com> >Date: 2008/02/05 Tue PM 12:52:06 CST >To: RHelp <r-help at stat.math.ethz.ch> >Subject: [R] Samplingyou just can put 24 in there and then use replace=FALSE sample(rep(letters[1:2],c(12,12),24,replace=FALSE))>Hi there, > > I want to generate different samples using the >followindg code: > > >g<-sample(LETTERS[1:2], 24, replace=T) > > How can I specify that I need 12 "A"s and 12 "B"s? > >Thank you, > >Judith > > > ____________________________________________________________________________________ >Be a better friend, newshound, and > >______________________________________________ >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.
> I want to generate different samples using the >followindg code: > >g<-sample(LETTERS[1:2], 24, replace=T) > > How can I specify that I need 12 "A"s and 12 "B"s?I introduced the concept of "sampling with minimal replacement" into the S-PLUS version of sample to handle things like this: sample(LETTERS[1:2], 24, minimal = T) This is very useful in variance reduction applications, to approximately stratify but with introducing bias. I'd like to see this in R. I'll raise a related issue - sampling with unequal probabilities, without replacement. R does the wrong thing, in my opinion:> values <- sapply(1:1000, function(i) sample(1:3, size=2, prob = c(.5, .25, .25))) > table(values)values 1 2 3 834 574 592 The selection probabilities are not proportional to the specified probabilities. In contrast, in S-PLUS:> values <- sapply(1:1000, function(i) sample(1:3, size=2, prob = c(.5, .25, .25))) > table(values)1 2 3 1000 501 499 You can specify minimal = FALSE to get the same behavior as R:> values <- sapply(1:1000, function(i) sample(1:3, size=2, prob = c(.5, .25, .25), minimal = F)) > table(values)1 2 3 844 592 564 There is a reason this is associated with the concept of sampling with minimal replacement. Consider for example: sample(1:4, size = 3, prob = 1:4/10) The expected frequencies of (1,2,3,4) should be proportional to size*prob = c(.3,.6,.9,1.2). That isn't possible when sampling without replacement. Sampling with minimal replacement allows this; observation 4 is included in every sample, and is included twice in 20% of the samples. Tim Hesterberg Disclaimer - these are my opinions, not those of my employer. =======================================================| Tim Hesterberg Senior Research Scientist | | timh at insightful.com Insightful Corp. | | (206)802-2319 1700 Westlake Ave. N, Suite 500 | | (206)283-8691 (fax) Seattle, WA 98109-3044, U.S.A. | | www.insightful.com/Hesterberg | =======================================================I'll teach short courses: Advanced Programming in S-PLUS: San Antonio TX, March 26-27, 2008. Bootstrap Methods and Permutation Tests: San Antonio, March 28, 2008.
Seemingly Similar Threads
- Replecing empty values with a letter (continuation)
- Multiple lines with a different color assigned to each line (corrected code)
- Ordering the levels of a vector
- Regular expression to define contents between parentheses
- Using the 'by' function within a 'for' loop