Re: [R] a quick question about "rbinom"

2012-01-05 Thread David Winsemius


On Jan 5, 2012, at 12:56 AM, Bert Gunter wrote:


David!
...


It means the author either doesn't know the 'sample' function, or  
wants you

to understand how to use 'rbinom', or that's just the way she thinks.


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]



C.f.

X1<-sample( c("A","B"), n,  prob= c(0.6, 0.4))
X2<-sample( c("C","D"), n,  prob= c(0.6 ,0.4))

... are wrong for n>1 !
e.g.

X1<-sample( c("A","B"), 5,  prob= c(0.6, 0.4))

Error in sample(c("A", "B"), 5, prob = c(0.6, 0.4)) :
 cannot take a sample larger than the population when 'replace =  
FALSE'


The perils of untested code. Thanks, Bert.



## You need to add the replace=TRUE argument

X1<-sample( c("A","B"), 5,  prob= c(0.6, 0.4),replace=TRUE)
X1

[1] "A" "A" "B" "A" "B"

Cheers,
Bert









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.




Sorry for the blank message.
--

David Winsemius, MD
West Hartford, CT

__
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.




--

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


David Winsemius, MD
West Hartford, CT

__
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.


Re: [R] a quick question about "rbinom"

2012-01-04 Thread Bert Gunter
David!
...
>
> It means the author either doesn't know the 'sample' function, or wants you
> to understand how to use 'rbinom', or that's just the way she thinks.
>
>>> 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]
>
>
> C.f.
>
> X1<-sample( c("A","B"), n,  prob= c(0.6, 0.4))
> X2<-sample( c("C","D"), n,  prob= c(0.6 ,0.4))
... are wrong for n>1 !
e.g.
> X1<-sample( c("A","B"), 5,  prob= c(0.6, 0.4))
Error in sample(c("A", "B"), 5, prob = c(0.6, 0.4)) :
  cannot take a sample larger than the population when 'replace = FALSE'

## You need to add the replace=TRUE argument
> X1<-sample( c("A","B"), 5,  prob= c(0.6, 0.4),replace=TRUE)
> X1
[1] "A" "A" "B" "A" "B"

Cheers,
Bert





>
>>>
>>> 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.
>>
>>
> Sorry for the blank message.
> --
>
> David Winsemius, MD
> West Hartford, CT
>
> __
> 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.



-- 

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

__
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.


Re: [R] a quick question about "rbinom"

2012-01-04 Thread David Winsemius


On Jan 4, 2012, at 8:46 PM, David Winsemius wrote:



On Jan 4, 2012, at 7:43 PM, Robert Baer wrote:


-Original Message- From: lynn.tsai
Sent: Wednesday, January 04, 2012 3:38 PM
To: r-help@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.


It means the author either doesn't know the 'sample' function, or  
wants you to understand how to use 'rbinom', or that's just the way  
she thinks.



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]


C.f.

X1<-sample( c("A","B"), n,  prob= c(0.6, 0.4))
X2<-sample( c("C","D"), n,  prob= c(0.6 ,0.4))



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.



Sorry for the blank message.
--

David Winsemius, MD
West Hartford, CT

__
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.


Re: [R] a quick question about "rbinom"

2012-01-04 Thread lynn.tsai
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.

__
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.


Re: [R] a quick question about "rbinom"

2012-01-04 Thread David Winsemius


On Jan 4, 2012, at 7:43 PM, Robert Baer wrote:


-Original Message- From: lynn.tsai
Sent: Wednesday, January 04, 2012 3:38 PM
To: r-help@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

__
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.


David Winsemius, MD
West Hartford, CT

__
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.


Re: [R] a quick question about "rbinom"

2012-01-04 Thread Robert Baer
-Original Message- 
From: lynn.tsai

Sent: Wednesday, January 04, 2012 3:38 PM
To: r-help@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

__
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.


Re: [R] a quick question about "rbinom"

2012-01-04 Thread David Winsemius


On Jan 4, 2012, at 5:31 PM, Justin Haynes wrote:


homework or not,

?rbinom

should be plenty.


I didn't think so. I thought that the answer was:

?"["

--  
David.




On Wed, Jan 4, 2012 at 1:38 PM, lynn.tsai   
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]


--






David Winsemius, MD
West Hartford, CT

__
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.


Re: [R] a quick question about "rbinom"

2012-01-04 Thread Steve Lianoglou
A direct answer to your question:

On Wed, Jan 4, 2012 at 4:38 PM, lynn.tsai  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

__
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.


Re: [R] a quick question about "rbinom"

2012-01-04 Thread Justin Haynes
homework or not,

?rbinom

should be plenty.




On Wed, Jan 4, 2012 at 1:38 PM, lynn.tsai  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]]

__
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.


Re: [R] a quick question about "rbinom"

2012-01-04 Thread Bert Gunter
Homework?

If not, context?

-- Bert

On Wed, Jan 4, 2012 at 1:38 PM, lynn.tsai  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.



-- 

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

__
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.


[R] a quick question about "rbinom"

2012-01-04 Thread lynn.tsai
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.