Hi everyone!
I have a for loop that applies a function ("f_richards") on a value ("vol") and sums the result of the function to the result of the previous loop (it is a function that calculates an increment based on the initial volume).
The code should perform a different time of loops for each row, where the number of loops is specified in the column "diff". The aim is to fill the last column (vol2020) with the result of the initial volume + n increments.
I used it on small data frames and it works properly but when I use it on bigger data frames (360 and 380 rows) it stops at a certain point (67th and 79th row). An error occurs:
Error in x[[jj]][iseq] <- vjj : replacement has length zero
The loop is the following:
for( i in 1:nrow(crescitaHP1)){
for ( j in 1:crescitaHP1$diff[i]){
crescitaHP1[i,2+2*(j-1)+1]= f_richards(crescitaHP1[i,2+2*(j-1)], crescitaHP1[i,ncol(crescitaHP1)-1])
crescitaHP1[i,2+2*(j-1)+2]=crescitaHP1[i,2+2*(j-1)]+crescitaHP1[i, 2+2*(j-1)+1]
crescitaHP1[i,ncol(crescitaHP1)]<-crescitaHP1[i,2+2*(j-1)+2]
}}
and it is applied on a dataframe (crescitaHP1) which has the following columns:
diff vol 2*max(rip) blank columns ID sp_gov vol2020
where "diff" is the number of loops to perform per row, "vol" is the initial volume, the number of blank columns has the size useful to be compiled with increment and new volume for each loop, "sp_gov" is a parameter of the function "f_richards" and "vol2020" is the final field I want to be compiled.
Do you have any idea of why it suddenly stops? Thank you so much! Hope I managed to explain it clearly