Help please ! R programming basics ! vectors operations

Hello everyone,
I need to calculate cashoutflows as below :

1st rotation : if frost risk = 1 cashoutflows [i+1] = cc (-1 800)
2nd rotation : if frost risk =1 cashoutflows [i+1] = cc (-1 800)
3rd rotation : if frost risk =1 cashoutflows [i+1] = cc (-1 800)
4th rotation : if frost risk =1 cashoutflows [i+1] = rc (-11 000)
The idea is like i have 3 tickets of "cc", when it is sold out i need to use "rc"

I appreciate your help
Thank you

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)
set.seed(1)
Myfunction <-runif(n=length(time), min=0,max=1)

#Frost_Risk
frost = rep(0, length(time))
p=1/15
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<-c(66)
#Cash inflows

cashinflows<- vector("numeric",300)
cashinflows<-growth_Rsk[1:300]*price
#Cash outflows

cashoutflows<- vector("numeric",300)
for(i in seq_along(frost)){
if( frost[i]==1 ){
i =i+1
cashoutflows[i]<-cc
}else{cashoutflows[i]==0
}
}

Its hard for me to detect a question in here.

Please find here more explanations, I'm trying to use cc 3 times then use rc once :
is it possible to use only one vector cc in the logic below ?

cc <-c() (what i want to calculate)
w (1,2,3,4)
If w <=3 so cc = -1800
else cc = -11000

Thank you

(w <- c(1,2,3,4))
(cc <- ifelse(w<=3,-1800,11000))

?

Thank you @nirgrahamuk, buthow to link that with frost vector: i mean
when the 1st, seceond, third frost happen (frost=1) i should use -1800, then -11000 for the fourth one :
My results should be like :

cashoutflows

[1] 0 0 0 0 0 0 0 0 0 0 -1800 0 0
[14] 0 0 0 0 0 0 0 0 0 0 0 0 0
[27] 0 -1800 0 0 0 0 0 0 0 0 0 0 0
[40] 0 0 0 0 0 0 0 0 -1800 0 0 0 0
[53] 0 0 0 0 0 0 0 0 0 0 0 0 0
[66] 0 0 0 0 0 0 0 0 0 0 0 0 0
[79] 0 0 0 0 0 0 0 0 0 0 0 0 0
[92] 0 -1800 0 0 0 0 0 0 0 0 0 0 0
[105] 0 0 0 0 0 0 0 0 0 0 0 0 -11000
[118] 0 0 0 0 0 0 0 0 0 0 0 0 0
[131] 0 0 0 -1800 0 0 0 0 0 0 0 0 0
[144] 0 0 0 0 0 0 0 0 0 0 0 0 0
[157] 0 0 0 0 0 0 0 0 0 0 0 -1800 0
[170] 0 0 0 0 0 0 0 0 0 0 0 0 0
[183] 0 0 0 0 0 0 0 0 0 0 0 0 0
[196] 0 0 0 0 0 0 0 0 0 0 0 0 0
[209] 0 0 0 0 0 0 0 0 0 0 0 0 0
[222] 0 0 0 0 0 0 0 -1800 0 0 0 0 0
[235] 0 0 0 0 0 0 0 0 0 0 0 0 0
[248] 0 0 0 0 0 0 0 0 0 0 0 0 0
[261] 0 0 0 0 0 0 0 -11000 0 0 0 -1800 0
[274] 0 0 0 0 0 0 0 0 -1800 0 0 0 -1800
[287] 0 0 0 0 0 0 0 0 0 0 0 0 0
[300] -1800

this ?

frost_df <- structure(list(name = 1:300, value = c(0, 0, 0, 0, 0, 0, 0, 0, 
                                       0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 
                                       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 
                                       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 
                                       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                       0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 
                                       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                       0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                       0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 
                                       0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0)), row.names = c(NA, 
                                                                                                                -300L), class = c("tbl_df", "tbl", "data.frame"))
library(tidyverse)

mutate(frost_df,
       csum = cumsum(value)) %>%
  mutate(csum_mod =  (csum %% 4) ,
         v = ifelse(csum_mod>1,-1800,-11000),
         f1 = ifelse(value==1,v,0),
         f2 = cumsum(f1)) %>% print(n=200)

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.