[R] for loop should check the looping index !!
Tony Plate
tplate at acm.org
Fri Jan 13 23:17:31 CET 2006
Yep, you missed the fact that 2:1 generates the sequence c(2,1).
Personally, I'd excuse you for missing this, as the documentation for
seq says:
The operator ':' and the 'seq(from, to)' form generate the
sequence 'from, from+1, ..., to'.
Maybe I'm missing something, but I don't see anywhere on the help page
for seq and ":" any mention of the fact the seq() generates a descending
sequence if 'to' is less than 'from'.
In programming, *never* use a construct like 1:length(x) or 2:length(x),
always using something like seq(1,len=length(x)) (or simply
seq(len=length(x)), or seq(2, len=length(x)-1) or seq(along=x)[-1].
-- Tony Plate
johan Faux wrote:
> 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 ????
>
> thanks,
> johan
>
>
> ---------------------------------
>
> Got holiday prints? See all the ways to get quality prints in your hands ASAP.
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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
>
More information about the R-help
mailing list