lundi 17 février 2020

Randomly select number (without repetition) for each group in R

I have the following dataframe containing a variable "group" and a variable "number of elements per group"

group    elements
1        3
2        1
3        14
4        10
..       ..
..       ..
30       5

then I have a bunch of numbers going from 1 to (let's say) 30

when summing "numb" I would get 900. what I want to obtain is to randomly select a number (from 0 to 30) from 1-30 and assign it to each group until I fill the number of elements for that group. Each of those should appear 30 times in total.

thus, for group 1, I want to randomly select 3 number from 0 to 30 for group 2, 1 number from 0 to 30 etc. until I filled all of the groups.

the final table should look like this:

group     number(randomly selected)
1           7
1           20
1           7
2           4
3           21
3           20
...

any suggestions on how I can achieve this?




Aucun commentaire:

Enregistrer un commentaire