lundi 12 novembre 2018

Creating a function that randomly swaps units within two sub-populations in R

I want to construct a function called swapRandomly that randomly swaps units within two sub-populations where swapping is done per row of both sub-populations. i.e.: randomly decide whether or not to swap the ith entry of sub-population 1 and sub-population 2 (for all i).

I want to do this without some fancy build in function. Only from the basics.

Eg would be something like

pop(pop1 = c(1,2,3), pop2 = c(2,4,6))

and swapRandomly(pop) => newPop(pop1 = c(1,4,6), pop2 = c(2,2,3))

Would really appreciate it if someone could do this, thanks!




Aucun commentaire:

Enregistrer un commentaire