I'm working on creating a drought severity index and currently have the following code to produce monthly drought severity index (PDSI) values for each of 160 sites:
library(scPDSI)
library(SPEI)
library(datasets)
library(plyr)
#PDSI CALCULATIONS
All_data <- read.csv("https://raw.githubusercontent.com/gbream/MS_Research/main/Climate_Lat_PET_AWC.csv")
#Splitting into object by Site_ID
Split_all_data <- split(All_data, All_data$Site_ID)
PDSI_161 <- pdsi(P=Split_all_data[[161]]$Prec, PE=Split_all_data[[161]]$PET, AWC = 142.9)
lapply(PDSI_161$X, function(x) write.table(data.frame(x),"161_Final_PDSI_data.csv", sep=",", append=TRUE))
I can individually run any Site_ID below 161 (up to 156) without issue, but when I try any sites numbered 161 or above, I get the following error:
"PDSI_161 <- pdsi(P=Split_all_data[[161]]$Prec, PE=Split_all_data[[161]]$PET, AWC = 142.9)
Error in Split_all_data[[161]] : subscript out of bounds"
I understand what the error means, but can't find any reason that it would be giving this error as all of the list objects are identical in size (1476 rows/8 columns) and format. Also, there is no significance to the site number being higher than 156- they are only site ID's. Any help would be appreciated.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…