[R] Vector with zeros and ones
R. Michael Weylandt
michael.weylandt at gmail.com
Tue Apr 23 01:26:07 CEST 2013
On Mon, Apr 22, 2013 at 7:21 PM, Ayyappa <ayyappach at gmail.com> wrote:
> Dear group,
>
> I want to generate a vector of 10 elements that always has 20% zeroes, but with a random ordering of zeroes and ones. Can you please suggest a function to do that in R? I tried 'sample' function but the 20% zeros was not always guaranteed.
v <- numeric(10) # Make a vector of 10 elements
v[sample(10, 2)] <- 1 # Set two random elements to one
Best,
Michael
More information about the R-help
mailing list