的我一直在尋找了幾個小時的互聯網試圖解決以下錯誤:For循環錯誤 - 下標出界
Error in Dataset[i, Year] : subscript out of bounds
下面是我的代碼產生錯誤的部分:
for(i in (2*YF):1){
if(Dataset[i,Year] < 0){
Dataset[i,Total_Births] <- Dataset[i,Male_Births] + Dataset[i,Female_Births]
}else{
Dataset[i,Total_Births] <- with(Dataset, sum(Dataset[Female_Births > (i-AEB) & Female_Births <= (i-ABB),Female_Births]))
Dataset[i,Male_Births] <- MBR * Dataset[i,Total_Births]
Dataset[i,Female_Births] <- FBR * Dataset[i,Total_Births]
}
}
'Year'開始於500,結束於-500跳過0.
我需要從下往上填充列。
任何幫助將不勝感激。
以下是完整代碼:
#DECLARE PARAMETERS
YF <- 500 #Years Ago Flood Occurred
FBR <- .5 #Historical Female Birth Rate
MBR <- (1-FBR) #Historical Male Birth Rate
ABF <- 12 #Average Births per Female
AL <- 60 #Average Lifespan
NF <- 4 #Number of Females at Debark
NM <- 4 #Number of Males at Debark
NAD <- 40 #Noah's Age at Debark
NWA <- 40 #Emzara's (Noah's Wife) Age at Debark
SA <- 35 #Shem's (Noah's Son) Age at Debark
SWA <- 35 #Sedeqetelebab's (Shem's Wife) Age at Debark
HA <- 30 #Ham (Noah's Son) Age at Debark
HWA <- 30 #Ne'elatama'uk's (Ham's Wife) Age at Debark
JA <- 25 #Japheth (Noah's Son) Age at Debark
JWA <- 25 #Adataneses's (Japheth's Wife) Age at Debark
CP <- 20 #Current Population of World Today
ABB <- 18 #Age Begin Births
AEB <- 30 #Age End Births
###CREATE MATRIX - YEAR###
YearA <- YF:1
YearB <- -1:-YF
Year <- c(YearA,YearB)
###POPULATE BIRTHS DATA###
BF_Male_Births <- rep.int(0,YF)
BF_Female_Births <- rep.int(0,YF)
BF_Male_Births[NAD] <- 1
BF_Female_Births[NWA] <- 1
BF_Male_Births[SA] <- 1
BF_Female_Births[SWA] <- 1
BF_Male_Births[HA] <- 1
BF_Female_Births[HWA] <- 1
BF_Male_Births[JA] <- 1
BF_Female_Births[JWA] <- 1
BF_Total_Births <- rep.int(0,YF)
AF_Male_Births <- rep.int(0,YF)
AF_Female_Births <- rep.int(0,YF)
AF_Total_Births <- c(rep.int(4,9),rep.int(3,3),rep.int(0,(YF-12))) #make parameters for 9,3,12
Male_Births <- c(AF_Male_Births,BF_Male_Births)
Female_Births <- c(AF_Female_Births,BF_Female_Births)
Total_Births <- c(AF_Total_Births,BF_Total_Births)
Total_Births <- rep.int(0,YF)
Births <- cbind(Male_Births,Female_Births,Total_Births)
Dataset <- cbind(Year,Births)
for(i in (2*YF):1){
if(Dataset[i,Year] < 0){
Dataset[i,Total_Births] <- Dataset[i,Male_Births] + Dataset[i,Female_Births]
}else{
Dataset[i,Total_Births] <- with(Dataset, sum(Dataset[Female_Births > (i-AEB) & Female_Births <= (i-ABB),Female_Births]))
Dataset[i,Male_Births] <- MBR * Dataset[i,Total_Births]
Dataset[i,Female_Births] <- FBR * Dataset[i,Total_Births]
}
}
你看過'數據集'嗎?它是4x1矩陣,填充1000.它只有4行,你試圖索引從1000到1.你是否在其他地方定義了「Total_Births」,「Male_Births」,「Female_Births」?它的代碼缺少其他變量,所以它不能運行。 – MrFlick
1)它是一個1000x2的矩陣2)「Year」,「Total_Births」,「Male_Births」,「Female_Births」是列名稱3)我不打算將它運行 - 如果你想 – JTeezee
我可以讓它運行好吧,在你粘貼在dim(數據集)上面的代碼中是4x1。很明顯,這不是你所期望的。 – MrFlick