samedi 2 janvier 2021

I'm stuck in a task with FOR and IF functions in R data programming

So currently i'm stuck in a problem around FOR and IF function.

I have a full working code like this:

#my data input
demand.variation=c(1,4,20,23,37)
demand.probability=c(0.81,0.85,0.89,0.93,0.97)
n.month=25

#generating result matrix
result=matrix(0,n.month,2)
colnames(result)=c("Month","Demand")

#filling result matrix with month index and random demand value
result[,1]=1:n.month
random=runif(n.month,0,1)

#return demand value based on random value
for (i in seq_along(random)) {
  if (random[i]>demand.probability[5]) result[i,2]=demand.variation[5]
  else if (random[i]>demand.probability[4]) result[i,2]=demand.variation[4]
  else if (random[i]>demand.probability[3]) result[i,2]=demand.variation[3]
  else if (random[i]>demand.probability[2]) result[i,2]=demand.variation[2]
  else if (random[i]>demand.probability[1]) result[i,2]=demand.variation[1]
  else result[i,2]=0
}

#final result
result

It is fully working with the result:

> result
      Month Demand
 [1,]     1      0
 [2,]     2      0
 [3,]     3      0
 [4,]     4      0
 [5,]     5      0
 [6,]     6      0
 [7,]     7      0
 [8,]     8      0
 [9,]     9      4
[10,]    10      0
[11,]    11      0
[12,]    12     23
[13,]    13     37
[14,]    14      0
[15,]    15      1
[16,]    16      0
[17,]    17      0
[18,]    18      0
[19,]    19      1
[20,]    20     20
[21,]    21      0
[22,]    22      0
[23,]    23      0
[24,]    24      0
[25,]    25      0

The thing is, i have to re-run the same code, but with different length and value of demand.variation and demand.probability, so i will have problem facing dozens variation for those 2 variables since the FOR and IF is manually entered.

My question is how do you generate a flexible code of FOR and IF statement to cover the job of #return demand value based on random value code.

footnote: demand.variation and demand.probability will always have the same vector length, and is correspondingly connected one to another, which means 1st value in demand.variation vector have the probability of the 1st value in demand.probability vector, and so on

demand.variation=c(1,4,20,23,37)
demand.probability=c(0.81,0.85,0.89,0.93,0.97)

I would be very grateful if you can help me on this.

Aucun commentaire:

Enregistrer un commentaire