Help pelasse ! Multiple If Statements in R

Hey Everyone,
I need to add another condition with same logic as for if i == 0 :
if "i" is multiple of 15+1 "16, 31, 46, 61.....286(max))
npv[index] <- -costs[index] i_rate[index]
}else{
npv[index] <- i_rate[index] * (npv[index-1] + price
(growth[index] -growth[index-1]))

Do you have some brilliant idea to integrate this to my calculations?
thank you :slight_smile: :slight_smile:

More explanations____________
Time from 0 to 300 : for each start of rotations or loops (15 years) i need to start with npv[index],
then for next 14 years npv[index] <- i_rate[index] * (npv[index-1] + price*(growth[index] -growth[index-1]))

time<-c(0:300)
growth<-c(0,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236)
price<-c(66)
i<-c(0.02)
costs <- vector("integer", 300)
costs[time %% 45 ==0] <- 1800
i_rate<-1/(1+i)^time
i_coeff<-1/(1+i)^time

npv <- vector("double", length(time))

for(i in time){
index = i + 1
if(i == 0){
npv[index] <- -costs[index] i_rate[index]
}else{
npv[index] <- i_rate[index] * (npv[index-1] + price
(growth[index] -growth[index-1]))
}
}

View(npv)

Hi,

Could you please explain in a bit more detail what calculations you are trying to do? Your code has some errors and won't run, and there are a few things that don't make sense.

I think it's easier also to start with a data frame instead of a list of values like so:

library(dplyr)

myData= data.frame(
  time = c(0:300),
  growth = c(0,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,
             0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,
             0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,
             0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,
             20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,
             37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,37,
             58,83,116,151,182,203,215,224,231,236,0,0,0,20,37,58,
             83,116,151,182,203,215,224,231,236,0,0,0,20,37,58,83,
             116,151,182,203,215,224,231,236,0,0,0,20,37,58,83,116,
             151,182,203,215,224,231,236,0,0,0,20,37,58,83,116,151,
             182,203,215,224,231,236,0,0,0,20,37,58,83,116,151,182,
             203,215,224,231,236,0,0,0,20,37,58,83,116,151,182,203,
             215,224,231,236,0,0,0,20,37,58,83,116,151,182,203,215,
             224,231,236,0,0,0,20,37,58,83,116,151,182,203,215,224,
             231,236,0,0,0,20,37,58,83,116,151,182,203,215,224,231,
             236,0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,
             0,0,0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,
             0,20,37,58,83,116,151,182,203,215,224,231,236,0,0,0,20,
             37,58,83,116,151,182,203,215,224,231,236),
  price = c(66),
  i = c(0.02),
  costs = 300
) %>% 
  mutate(
    costs = ifelse(time %% 45 == 0, 1800, costs),
    i_rate = 1/(1+i)^time,
    i_coeff = 1/(1+i)^time
  )

head(myData)
#>   time growth price    i costs    i_rate   i_coeff
#> 1    0      0    66 0.02  1800 1.0000000 1.0000000
#> 2    1      0    66 0.02   300 0.9803922 0.9803922
#> 3    2      0    66 0.02   300 0.9611688 0.9611688
#> 4    3      0    66 0.02   300 0.9423223 0.9423223
#> 5    4     20    66 0.02   300 0.9238454 0.9238454
#> 6    5     37    66 0.02   300 0.9057308 0.9057308

Created on 2021-01-25 by the reprex package (v0.3.0)

So with this table, try and explain what it is you like to calculate (give some examples before and after) and I'm sure there will be an easy way to do this.

PJ

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.