对于循环错误 - 下标超出界限

时间:2014-07-10 01:55:27

标签: r loops for-loop matrix subscript

我一直在网上搜索数小时试图解决以下错误:

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]
    }
}

'年'以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]
  }
}

1 个答案:

答案 0 :(得分:0)

执行Dataset[i,Year]时,iYear都会被视为指标。由于year是数字,因此子集运算符假定您希望返回这些列值。但是,Year是一个值为500:-500的向量,您只有4列,这就是为什么会出现越界错误的原因。这有点令人困惑,因为您在矩阵之外定义了Year向量,并且矩阵内有Year列。

如果使用字符向量进行索引,则它将匹配行名称和列名称。所以认为你的意思是

Dataset[i,"Year"]

您索引Dataset的所有其他地方也是如此。现在,没有引号,它使用你在矩阵之外定义的值,这不是你想要的。

此外,您不能将with()与矩阵一起使用。 with()应与data.frames或环境或列表一起使用,而Dataset不是其中之一。这更接近正确的语法

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"] <- sum(Dataset[Dataset[, "Female_Births"] > (i-AEB) & Dataset[,"Female_Births"] <= (i-ABB),"Female_Births"])
    Dataset[i,"Male_Births"] <- MBR * Dataset[i,"Total_Births"]
    Dataset[i,"Female_Births"] <- FBR * Dataset[i,"Total_Births"]
  }
}

但似乎你可能仍然有一些逻辑错误。