Re: [R] problem with function "rep"

From: Charilaos Skiadas <cskiadas_at_gmail.com>
Date: Thu, 12 Jun 2008 13:39:20 -0400

I believe this might do what you want a bit faster. I replaced the while loop with something that is likely to be faster. I saw no reason for the rounding you were doing, better to use as.integer at the end.

test <- function(t){

x <- rexp(t,0.1)
while(sum(x) <= t) {

        x <- c(x, rexp(t,0.1))
}

x <- as.integer(x/0.0001)
x <- x[cumsum(x)<=10000*t]
y <- rnorm(length(x),0,1)
t <- rep(y,x)

return(list(sum(x),length(t)))
}
test(100)

Btw, it is not very wise to call a variable "t", given that that's a built in function.

Haris Skiadas
Department of Mathematics and Computer Science Hanover College

On Jun 12, 2008, at 11:27 AM, Julien Hunt wrote:

> Hi I believe this should provide an example of the confusing behavior.
>
> Run this with t=100 for example:
>
> test=function(t){
>
> x=c()
> while(sum(x)<=t){
> ###I simply generate some numbers from an
> exponential until the sum of these numbers gets
> to 100(without loss of generality)
> x=c(x,round(rexp(1,0.1),4))
> }
> x=x/0.0001
>
> y=rnorm(length(x),0,1)
> t=rep(y,x)
>
> return(sum(x),length(t))
>
> }
>
> The intuition is that sum(x) and length(t) should
> be the same. furthermore, rounding x seems since
> all is done for it to be an integer.
> Nevertheless, I will try Berwin Turlach's method.
> Regards,
> Julien
>
> At 17:01 12/06/2008, Erik Iverson wrote:
>> We need a reproducible example of this to tell
>> you what is going on. Find a small example that
>> exhibits the confusing behavior, and share it with the list.
>>
>> Julien Hunt wrote:
>>> To whom it may concern,
>>> I am currently writing a program where I need to use function rep.
>>> The results I get are quite confusing. Given
>>> two vectors A and B, I want to replicate a[1]
>>> b[1] times, a[2] b[2] times and so on.
>>> All the entries of vector B are positive integers.
>>> My problem comes from the fact that if I sum up
>>> all the elements of B, I get a certain
>>> value x(for example 10000). And if i calculate
>>> the length of the vector obtained after
>>> replication, I dont always get x(10000) but
>>> sometimes I get x sometimes I get 9999 instead of 10000.
>>> Has this problem been reported before? Do you
>>> need more information on my specific program.
>>> Thanks for your time and help,
>>> Best regards,
>>> Julien Hunt
>>> Julien Hunt,
>>> PhD student and teaching assistant,
>>> Institute of Statistics,
>>> Université Catholique de Louvain,
>>> Voie du Roman pays 20
>>> B-1348 Louvain-La-Neuve,
>>> Belgium
>>> E-mail: Julien.Hunt_at_uclouvain.be
>>> Tel: +32 10 / 47 94 01
>>> *****************************************************
>>>
>>> [[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.
>>
>> Julien Hunt,
>> PhD student and teaching assistant,
>> Institute of Statistics,
>> Université Catholique de Louvain,
>> Voie du Roman pays 20
>> B-1348 Louvain-La-Neuve,
>> Belgium
>>
>> E-mail: Julien.Hunt_at_uclouvain.be
>> Tel: +32 10 / 47 94 01



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. Received on Thu 12 Jun 2008 - 17:45:04 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 12 Jun 2008 - 18:30:40 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive