mardi 28 avril 2015

Argument error length in for loop nested conditional?

I am currently returning the error

Error in if (sandc[which(Index == i)] == 1) if (sandc[which(Index == i)] ==  : 
  argument is of length zero

when I try and run the following code:

weather<-read.xls("finaldata4.xls")
attach(weather)
length(Low)

for(i in 2:length(low)){
 w<-rep(0,length(low))
  w[1]<-1000;
  if(sandc[which(Index==i)]==1){
    w[i]<-gross[which(Index==i)]*w[i-1]
  }
  else{
    w[i]<-(gross[which(Index==i)]*-1)*w[i-1]
  }
}

It's unclear to me what is going, wrong, but I suspect it is something to do with the indexing. The dataset just has a simple index (1 to 1254) and several variables, which I have attached (like gross, and sandc).

Any ideas where this error is coming from?

Aucun commentaire:

Enregistrer un commentaire