[R] Error: argument of length 0

Duncan Murdoch murdoch.duncan at gmail.com
Thu Mar 29 23:37:36 CEST 2012


On 12-03-29 2:57 PM, ens wrote:
> Problem:
>
>   I keep getting: "Error in (index - 9):index : argument of length 0" when I
> run this code in an automated loop, but when I step through it by hand, I
> have no problems. It keep crashing when i<-10. I cannot figure this out to
> save my life! Please help and THANK YOU in advance!

Presumably the index variable is of length zero.  You haven't given us a 
reproducible example, but I would guess you have index set to something 
like index_year[0] at the time the error happens.  For example:

 > x <- 1:10
 > index <- x[0]
 > (index-9):index
Error in (index - 9):index : argument of length 0

Duncan Murdoch

>
> variables:
>
> FM100 is a matrix of columns: year, day (all years set to 366 days), region
> 1, region 2... region N
> columnID<- region # +2
> numberFires is the number of fires that occurred in the region (the code
> assures there are more than 0 fires)
> years is a vector of fire years as pertains to each fire counted in
> numberFires
> explanatory.variables$DOY is a vector of Day-of-Year for each fires start
> day
>
> code:
>
>      #Read FM100 file
>      if (REGION_SCALE ==
> "psa"){FM100<-read.csv("climaggregates/fm100_psa.csv",
>
> header=FALSE,col.names=c("year","day",psa$OBJECTID))}
>      if (REGION_SCALE ==
> "ecosection"){FM100<-read.csv("climaggregates/fm100_ecp.csv",
>
> header=FALSE,col.names=c("year","day",ecosection$OBJECTID))}
>      for (i in 1:numberFires){
>        index_yr<-which(FM100$year==years[i])
>
>        # file default 366 days/year w/ Feb 29 set to NAN, but DOY accounts
> for leap years when
>        # assigning Day-of-year for mm-dd-yyyy
>        if (years[i]%%4 != 0) {index_day<-explanatory.variables$DOY[i]+1}
>        if (years[i]%%4 == 0) {index_day<-explanatory.variables$DOY[i]}
>        index<-index_yr[index_day]
>
>        # 10 day antecedent FM100 average
>        explanatory.variables$FM100[i]<-mean(FM100[(index-9):index,columnID])
>      }
>      rm(FM100)
>      print("FM100 variables created")
>
> What's in each variable:
>
>> numberFires
> [1] 73
>> index_yr
>   6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969
> 6970 6971 6972 6973 6974 6975 6976 6977 .... [index: 346] 7300 7301 7302
> 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317
> 7318 7319 7320
>> index_day
> [1] 235
>> index
> [1] 7189
>> FM100[(index-9):index,columnID]
>   [1] 14.6560 14.3190 13.7730 12.0370 11.1230 10.9360 11.1180 10.3010 10.2910
> 9.5018
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Error-argument-of-length-0-tp4516511p4516511.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list