Help pelasse ! R coding if else function

Hey everyone,
I need help to calculate #Cashflows
#Cashflows
cashflows=(growth_Rsk*price)+ (cc+rc i need to code this part)

The idea is that I have three tickets of cc to use every rotation and just one ticket of rc to use every 3 rotations (when cc are sold out):

I want to code :

if growth risk interuppted by frost and restarted from 0 : use cc (-1800) for three rotations (repeat 3 times)
when cc tickets sold out :
use cc use rc (-11 000) for the fourth rotation

Thank you, I appreciate your help

time<-c(1:300)
growth<-c(0,0,0,20,17,21,25,33,35,31,21,12,9,7,5,0,0,0,20,17,21,25,33,35,31,21,12,9,7,5,0,0,0,20,17,21,25,33,35,31,21,12,9,7,5,0,0,0,20,17,21,25,33,35,31,21,12,9,7,5,0,0,0,20,17,21,25,33,35,31,21,12,9,7,5,0,0,0,20,17,21,25,33,35,31,21,12,9,7,5,0,0,0,20,17,21,25,33,35,31,21,12,9,7,5,0,0,0,20,17,21,25,33,35,31,21,12,9,7,5,0,0,0,20,17,21,25,33,35,31,21,12,9,7,5,0,0,0,20,17,21,25,33,35,31,21,12,9,7,5,0,0,0,20,17,21,25,33,35,31,21,12,9,7,5,0,0,0,20,17,21,25,33,35,31,21,12,9,7,5,0,0,0,20,17,21,25,33,35,31,21,12,9,7,5,0,0,0,20,17,21,25,33,35,31,21,12,9,7,5,0,0,0,20,17,21,25,33,35,31,21,12,9,7,5,0,0,0,20,17,21,25,33,35,31,21,12,9,7,5,0,0,0,20,17,21,25,33,35,31,21,12,9,7,5,0,0,0,20,17,21,25,33,35,31,21,12,9,7,5,0,0,0,20,17,21,25,33,35,31,21,12,9,7,5,0,0,0,20,17,21,25,33,35,31,21,12,9,7,5)
Myfunction <-runif(n=length(time), min=0,max=1)

#Frost_Risk
frost = rep(0, length(time))
p=1/10
for (i in 1:length(Myfunction)){
if (Myfunction[i]<=p) {frost[i] = 1}
else{frost[i] = 0}
}
print(frost)

#Growth under frost risk
growth_Rsk<- vector("numeric",300)
index<-0
for(i in seq_along(frost)){
if( frost[i]==0 )index<-index+1
else index<-1
growth_Rsk[i+1]<-growth[index]
}
#Coppicing costs
cc<-(-1800)

#Replanting costs
rc<-(-11000)

#Discount rate/coeff
a<-c(0.02)
a_rate<-1/(1+a)^time

#Price
price<-66

#Cashflows
cashflows<- vector("numeric",300)
.
.
.
.

#Discounted Cashflows
discounted_cashflows<-cashflows*a_rate
#Net Present Value
npv<-cumsum(discounted_cashflows)
print(npv)

Why do you need a loop? Can't you just use a data.frame and then use dplyr?

Thank you for response, i think that using data.frame in my case is not the best solution. For the next step of my simulation, i need to generate 20 000 random numbers (frost event) with my runif function. So the rows will change randomly in next calculations.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.