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

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Sat 14 Jan 2006 - 09:16:59 EST

johan Faux <johanfaux@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@biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Sat Jan 14 09:23:49 2006

This archive was generated by hypermail 2.1.8 : Sat 14 Jan 2006 - 14:06:25 EST