Dear R experts, I have a problem with a function I wrote. The fuction looks like this: series<-function(x,s){ foo<-list(); ind3<-integer(); for (j in diff){ for (i in 1:(n-12)){ if (!(x[i,j]==0)&!(x[i+1,j]==0)&!(x[i+2,j]==0)&!(x[i+3,j]==0)&!(x[i+4,j]==0)&!(x[i+5,j]==0)&!(x[i+6,j]==0) &!(x[i+7,j]==0)&!(x[i+8,j]==0)&!(x[i+9,j]==0)&!(x[i+10,j]==0)&!(x[i+11,j]==0)&!(x[i+12,j]==0)){cont<-i; break} else cont<-i }; if (cont>92) {ind3<-c(ind3,j); foo[[j]]<-NULL;next} else { año<-floor(cont/12); mes<-(cont-12*año); a<-(1999+año); foo[[j]]<-ts(x[(cont):nrow(x),j],frequency=12,start=c(a,mes))}}; That's the first part, then ind4<-integer(); for (m in setdiff(diff,ind3)){ nn<-length(foo[[m]]); prinf<-median(foo[[m]][1:12]); prsup<-median(foo[[m]][(nn-12):nn]); if (xor((prsup/prinf)>s,(prinf/prsup)>s)) {ind4<-c(ind4,m)}}; crit3<<-ind4; So far so good (aparently). I mean, the "crit3" set it's ok. But, the next paragraph: ind5<-integer(); for (k in ind4){ print(k); #to know when the error happens dife<-numeric(); l<-length(foo[[k]]); print(l); print(foo[[K]]) #to know why the error happens for (i in 9:(l-8)){ minf<-median(foo[[k]][(i-8):(i-1)]); msup<-median(foo[[k]][(i+1):(i+8)]); dife[i]<-abs(ceiling((msup-minf)))}; st<-which(dife==max(abs(dife),na.rm=T)); if (length(st)>1){st<-st[length(st)]}; if ((l-st)<24){ind5<-c(ind5,m);foo[[k]]<-NULL} else{ agno<-(start(foo[[k]])[1]+floor(st/12)); mes<-(start(foo[[k]])[2]+ (st-12*floor(st/12))); if (mes>12){ agno<-(agno+1);mes<-(mes%%12)}; ss<-foo[[k]][(st+1):l]; foo[[k]]<-ts(ss,frequency=12,start=c(agno,mes))}} When the code access the series foo[[k]] it does not match the series foo[[k]] outside the loop. I know this because the I've checked it out. I've noticed this because the function throws me an error that says : "Error en median.default(foo[[k]][(i - 8):(i - 1)]) : need numeric data" The error occurs when m=550, because IN the loop foo[[550]]=NULL, but outside the loop foo[[550]] is a normal time series. I know this because I printed foo[[550]] before entering the loop. My surprise did not stop there, because from the second index on none of the internal loop series match the "outer" loop series... Please don't ask me why I'm doing this, I just need it solved. Please someone help me!!!!!!! [[alternative HTML version deleted]]