jeudi 11 mars 2021

R How to generate sample number by splitting a given number in a specific times with random ratio

Goal to create a random dataset by distribute a designed TOTAL number into a specific parts (times) with a random ratio .

e.g.

Given number 
=100

Divide in a specific times
n= 5

Random ratio : 5 
ratio = c(0.1,0.04,0.01,0.3,0.55) # assume there is a function to randomly generated this

Respectively equal to 
100*0.1= 10 ,
100*0.04 = 4,
100*0.01= 1,
100*0.3= 30,
100*0.55= 55

then the total after divided by this ratio will return back to given number 100.

Solution are separately recognised but don't know how to combine them in a efficient way :

# generate random number within a range
sample(x:xx)
# generate number by only 1 decided ratio but NOT divided evenly based on a specific time .
seq(from=0, to=1, by=0.1) 



Aucun commentaire:

Enregistrer un commentaire