That's what I did. 'branches' was shown at the top.

branches = c(5,6,5,5,5)

I tested this. When I copy and paste into R 10.0 I get the result in the 
post. Perhaps I should reinstall R. I guess I don't see how much more 
narrow I can get than this.

  i     iInd = 1
  for(i in 1:length(branches)) {
    print((1:branches[i])+iInd-1)   # iInd is a position shift of the index
    ni = branches[i]
    print(i)
    print(ni)
    print(c(ni,rep(i,times=ni)))
# ... some interesting other stuff for my project that gets wrecked 
because of this issue
    iInd = iInd + branches[i]
}



Erik Iverson-3 [via R] wrote:
> Cannot reproduce, what is branches?  If you can narrow it down to a
> "commented, minimal, self-contained, reproducible" example, you're far
> more likely to get help from the list.
>
> dkStevens wrote:
>
> > I'm observing odd behavior of the rep(...) procedure when using 
> variables as
> > parameters in a loop. Here's a simple loop on a vector 'branches' 
> that is
> > c(5,6,5,5,5). The statement in question is          
> > print(c(ni,rep(i,times=ni)))
> >
> > that works properly first time through the loop but the second time, 
> when
> > branches[2] = 6, only prints 5 values of i.
> >
> > Any ideas, anyone?
> >
> >   iInd = 1
> >   for(i in 1:length(branches)) {
> >     print((1:branches[i])+iInd-1)   # iInd is a position shift of 
> the index
> >     ni = branches[i]
> >     print(i)
> >     print(ni)
> >     print(c(ni,rep(i,times=ni)))
> > # ... some interesting other stuff for my project that gets wrecked 
> because
> > of this issue
> >     iInd = iInd + branches[i]
> > }
> >
> > # first pass through loop
> > [1] 1 2 3 4 5             # branches[1] + iInd - 1 content
> > [1] 1                        # i value to repeat 5 times
> > [1] 5                        # ni = 5 on 1st pass
> > [1] 5 1 1 1 1 1           # five values - 1 1 1 1 1 - OK
> >
> > # second pass through loop
> > [1]  6  7  8  9 10 11   # branches[2] + iInd - 1 content
> > [1] 2                       # i value to repeat 6 times
> > [1] 6                       # ni = 6 on 2nd pass
> > [1] 6 2 2 2 2 2          # 6 'twos' but only shows 5 'twos'
> > print(c(ni,rep(i,times=ni))) - why not 6?
> >
> >
>
> ______________________________________________
> [hidden email] 
> <http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1560566&i=0> 
> mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
> ------------------------------------------------------------------------
> View message @ 
> http://n4.nabble.com/Funny-result-from-rep-procedure-tp1560547p1560566.html 
>
> To unsubscribe from Funny result from rep(...) procedure, click here 
> < (link removed) =>. 
>
>

-- 
David K Stevens, P.E., Ph.D., Professor
Civil and Environmental Engineering
Utah Water Research Laboratory
8200 Old Main Hill
Logan, UT  84322-8200
435 797 3229 - voice
435 797 1363 - fax
david.stev...@usu.edu




-- 
View this message in context: 
http://n4.nabble.com/Funny-result-from-rep-procedure-tp1560547p1560641.html
Sent from the R help mailing list archive at Nabble.com.

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to