[R] for loop should check the looping index !!

Peter Dalgaard p.dalgaard at biostat.ku.dk
Fri Jan 13 23:16:59 CET 2006


johan Faux <johanfaux at yahoo.com> writes:

> Hello ,
>    
>   a<-c(1)
>   for(i in 2:length(a))
>       do.something with a[[i]]
>    
>   I get :
>   Error in a[[i]] : subscript out of bounds
>    
>   Am I missing something here?  Doesnt R check the value of i inside "for" and if the condition is not tru, dont do anything ????

Well,

> 2:1
[1] 2 1


The for loop as such has no idea what you're going to do with the
index variable, so will faithfully try to do.something with a[[2]]

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907




More information about the R-help mailing list