[R] Vector of ones and zeros

Rolf Turner r.turner at auckland.ac.nz
Wed Nov 19 23:57:28 CET 2008


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



More information about the R-help mailing list