for loop should check the looping index !!

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

for loop should check the looping index !!

johan Faux
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]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: for loop should check the looping index !!

Peter Dalgaard
johan Faux <[hidden email]> 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
~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: for loop should check the looping index !!

Tony Plate-3
In reply to this post by johan Faux
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]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html