[R] Vector of ones and zeros

Yihui Xie xieyihui at gmail.com
Fri Nov 21 06:39:31 CET 2008


or rbinom(n, size = 1, prob)

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: +86-(0)10-82509086 Fax: +86-(0)10-82509086
Mobile: +86-15810805877
Homepage: http://www.yihui.name
School of Statistics, Room 1037, Mingde Main Building,
Renmin University of China, Beijing, 100872, China



On Thu, Nov 20, 2008 at 6:57 AM, Rolf Turner <r.turner at auckland.ac.nz> wrote:
>
> On 20/11/2008, at 11:47 AM, hassen62 at voila.fr wrote:
>
>> Dear R people,
>> what functions generate respectively vectors with each element is
>> respectively zero and one.
>> sorry for my credulous questions and many thanks in advance.
>
> If you want a *random* (i.i.d.) vector of zeroes and ones, then the
> following sort of thing
> will do it:
>
>        sample(0:1,42,TRUE) # TRUE is necessary for sampling with
> replacement.
>        sample(0:1,42,TRUE,prob=c(0.25,0.75)) # 1/4 zeroes and 3/4 ones on
> average; the default is equal probabilities.
>
> If you want some non-random structure, or if you want serial dependence
> between the entries
> you need to make your question more explicit.
>
> *Do* read the posting guide!
>
>        cheers,
>
>                Rolf Turner
>
> ######################################################################
> Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list