[R] a quick question about "rbinom"

Bert Gunter gunter.berton at gene.com
Thu Jan 5 06:56:35 CET 2012


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



More information about the R-help mailing list