Hello, I have the following code using rbinom, but I don't understand what *"+1"* means in the code. Could someone help? Thanks so much,> X1<-c("A","B")[rbinom(n,1,0.6)+1] > X2<-c("C","D")[rbinom(n,1,0.1)+1]-- View this message in context: http://r.789695.n4.nabble.com/a-quick-question-about-rbinom-tp4262977p4262977.html Sent from the R help mailing list archive at Nabble.com.
Homework? If not, context? -- Bert On Wed, Jan 4, 2012 at 1:38 PM, lynn.tsai <vernal.lyn at gmail.com> wrote:> Hello, I have the following code using rbinom, but I don't understand what > *"+1"* means in the code. Could someone help? Thanks so much, > >> X1<-c("A","B")[rbinom(n,1,0.6)+1] >> X2<-c("C","D")[rbinom(n,1,0.1)+1] > > -- > View this message in context: http://r.789695.n4.nabble.com/a-quick-question-about-rbinom-tp4262977p4262977.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
homework or not, ?rbinom should be plenty. On Wed, Jan 4, 2012 at 1:38 PM, lynn.tsai <vernal.lyn@gmail.com> wrote:> Hello, I have the following code using rbinom, but I don't understand what > *"+1"* means in the code. Could someone help? Thanks so much, > > > X1<-c("A","B")[rbinom(n,1,0.6)+1] > > X2<-c("C","D")[rbinom(n,1,0.1)+1] > > -- > View this message in context: > http://r.789695.n4.nabble.com/a-quick-question-about-rbinom-tp4262977p4262977.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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]]
A direct answer to your question: On Wed, Jan 4, 2012 at 4:38 PM, lynn.tsai <vernal.lyn at gmail.com> wrote:> Hello, I have the following code using rbinom, but I don't understand what > *"+1"* means in the code. Could someone help? Thanks so much, > >> X1<-c("A","B")[rbinom(n,1,0.6)+1] >> X2<-c("C","D")[rbinom(n,1,0.1)+1]Is that the "+1" is adding 1 to the result returned from rbinom(). Look at what happens when you do: R> c(1, 3, 5, 10) + 1 ?Tada! If you're just learning R, you'll find it useful to deconstruct "complex" commands into their "unit" pieces, if you catch my meaning ... look at what rbinom() returns, look at what it returns when you "+1" it, then look at what you get when you use that whole expression inside the "[]" to select elements in `c("A", "B")` You will see the light ... HTH, -steve -- Steve Lianoglou Graduate Student: Computational Systems Biology ?| Memorial Sloan-Kettering Cancer Center ?| Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
-----Original Message----- From: lynn.tsai Sent: Wednesday, January 04, 2012 3:38 PM To: r-help at r-project.org Subject: [R] a quick question about "rbinom" Hello, I have the following code using rbinom, but I don't understand what *"+1"* means in the code. Could someone help? Thanks so much,> X1<-c("A","B")[rbinom(n,1,0.6)+1] > X2<-c("C","D")[rbinom(n,1,0.1)+1]It mean index the vector as 1's and 2's (1st and 2nd item in list) rather than 0's and 1's because R indices start at 1, not 0. ------------------------------------------ Robert W. Baer, Ph.D. Professor of Physiology Kirksville College of Osteopathic Medicine A. T. Still University of Health Sciences 800 W. Jefferson St. Kirksville, MO 63501 660-626-2322 FAX 660-626-2965
Hi, thank you all for replying. First, it's not a homework. I'm learning R and the CFA (configural frequency analysis). I am trying to understand this part of the code so that I can modify it and run a simulation for power analysis of CFA. Thanks again. -- View this message in context: http://r.789695.n4.nabble.com/a-quick-question-about-rbinom-tp4262977p4263919.html Sent from the R help mailing list archive at Nabble.com.