Calculating SPEI with .nc file data

Hi. I'm very new to R (coding in general, actually) and am trying to assess drought with the SPEI package. My data is from an .nc4 file with dimensions 720, 360, 120 (monthly data for 10 years). I have already done the PPT-PET. I now need to do the SPEI calculations but have run into a couple of problems. I keep getting this error:

Error in spei_value[ii, jj, kk] <- spei(D19410[ii, jj, kk], 1, na.rm = TRUE) :
number of items to replace is not a multiple of replacement length

Here's my code:

D<-ppt - evap ##calculation for PPT-PET

speiD <- array(data=NA, dim=c(720,360,120)) ##made empty array to store my SPEI data

for (ii in 1:720){
for (jj in 1:360){
for (kk in 1:120){
speiD[ii,jj,kk]<-spei(D[ii,jj,kk], 1, na.rm=TRUE)
}
}
}

Am I confusing my dimensions? Any help much appreciated!

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