I am needing to create a vector of length 1M with a hundred thousand values equal to 1 and the rest equal to 0, and then extract a sample of size 1000 from it and evaluate the ratio of each.
I am needing to create a vector of length 1M with a hundred thousand values equal to 1 and the rest equal to 0, and then extract a sample of size 1000 from it and evaluate the ratio of each.
You can use
sample()
to generate the sample:With this function you can generate from a sample vector
0:1
, an
number of values, important in this case usereplace=TRUE
to repeat values, the generation will be random with a uniform distribution, eventually you could assign a probability to each value, using the parameterprob
.The selection after a sample can also be done with
sample()
but this time to randomize the indices that will be used to select the data: