lundi 22 juin 2020

How to shuffle from a vector in R?

Suppose that we have the following vector that could change within a loop but with fixed length:

V = c(8,15,16,7,3)

I need to shuffle in each iteration m compenents at uniform from V. ( 1<=m<=5 & m is integer)

Then I will need to store those components in their respective positions at another vector K with the same length as V. The other positions will simply contain the value 0.

For example: let j be the current iteration and let assume that V will not change. some possible results could be :

m=3
---------------------
//
j=1
V= c(8,15,16,7,3)
K= c(0,15,0,7,3)   # 3 numbers are shuffled //


//
j=2
V= c(8,15,16,7,3)
K= c(8,0,0,7,3)   # 3 numbers are shuffled
 //
.
.
.

j=4
V= c(8,15,16,7,3)
K= c(0,0,16,7,3)   # 3 numbers are shuffled

I wish my question and the desired output is clear. Thank a you a lot for help!




Aucun commentaire:

Enregistrer un commentaire