[R] Funny result from rep(...) procedure

Henrik Bengtsson hb at stat.berkeley.edu
Thu Feb 18 19:56:40 CET 2010


What does

print(as.integer(branches))

give?  That is what rep() uses.

/H

On Thu, Feb 18, 2010 at 7:49 PM, dkStevens <david.stevens at usu.edu> wrote:
>
> I could send the entire bit of code but I was hoping that someone would
> recognize the issue from past experience. I may be an artifact of other
> parts of the code. I observed the problem in a larger context and cut
> out all that you see below. The comments were next to the results to
> clarify. Apparently I failed to be clear.
>
> Eik Vettorazzi [via R] wrote:
>> Sorry, not reproducible. This works for me (as expected):
>>
>> branches<-c(5,6)
>> 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]
>> }
>>
>>
>> dkStevens schrieb:
>>
>> > 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?
>> >
>> >
>> >
>>
>> --
>> Eik Vettorazzi
>> Institut für Medizinische Biometrie und Epidemiologie
>> Universitätsklinikum Hamburg-Eppendorf
>>
>> Martinistr. 52
>> 20246 Hamburg
>>
>> T ++49/40/7410-58243
>> F ++49/40/7410-57790
>>
>> ______________________________________________
>> [hidden email]
>> <http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1560587&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-tp1560547p1560587.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.stevens at usu.edu
>
>
>
>
> --
> View this message in context: http://n4.nabble.com/Funny-result-from-rep-procedure-tp1560547p1560643.html
> Sent from the R help mailing list archive at Nabble.com.
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at 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.
>
>



More information about the R-help mailing list