-2

I have been searching the internet for hours trying to resolve the following error:

Error in Dataset[i, Year] : subscript out of bounds

Below is the section of my code producing the error:

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' starts with 500 and ends with -500 skipping 0.

I need to populate the columns from the bottom up.

Any help would be much appreciated.

Below is the full code:

#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]
  }
}
JTeezee
  • 38
  • 1
  • 6
  • Did you look at `Dataset`? It's 4x1 matrix filled with 1000. It only has 4 rows and you're trying to index from 1000 to 1. Plus did you define "Total_Births","Male_Births","Female_Births" somewhere else? It code is missing other variables as well so it is not runnable. – MrFlick Jul 10 '14 at 02:05
  • 1)It's a 1000x2 matrix 2)"Year","Total_Births","Male_Births","Female_Births" are Column Names 3)I didn't intend it to be runnable - I can make it runnable if you want – JTeezee Jul 10 '14 at 02:11
  • Well, in the code you pasted above `dim(Dataset)` is 4x1. So clearly it is not what you expected. – MrFlick Jul 10 '14 at 02:18
  • It was most definately 1000x4 - Regardless I added the full code and removed the two lines that were causing confusion. – JTeezee Jul 10 '14 at 02:37

1 Answers1

0

When you do Dataset[i,Year], both i and Year are treated as indicies. Since year is numeric, the subsetting operator assumes that you want those column values returned. However, Year is a vector with the values 500:-500 and you only have 4 columns, which is why you get the out of bounds error. It's a bit confusing because you have a Year vector defined outside your matrix, and a Year column inside your matrix.

If you do indexing with a character vector, it will match on row names and column names. So think you meant

Dataset[i,"Year"]

Same goes for all the other places where you index Dataset. Right now, without the quotes, Its using the values you defined outside the matrix which is not what you want.

In addition, you cannot use with() with a matrix. with() should be used with data.frames or environments or lists, and Dataset isn't any of those. This is closer to proper syntax

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

but it seems like you may still have some logic errors in there.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Now I am getting the following error: `Error in eval(substitute(expr), data, enclos = parent.frame()) : numeric 'envir' arg not of length one` – JTeezee Jul 10 '14 at 02:48