mercredi 12 décembre 2018

R, generate a vector between 0 and 1, with certain length and certain number of Ones

I need to create a vector of length 4, such that 2 of the elements are 1, the other 2 are 0, and which number appears in a certain place is random.

For example, the first simulation is [1, 1, 0, 0]. The second simulation might be [1, 0, 1, 0] or something else.

I tried the code as follows:

 sample(x = c(0,1), size = 4, replace = TRUE, prob = 0.5)

but it simply returned a list with random numbers of 1 and 0.

Is it possible to make such a list without using for or while loops?




Aucun commentaire:

Enregistrer un commentaire