[R] avoiding loop

Charles C. Berry cberry at tajo.ucsd.edu
Sun Nov 1 19:00:54 CET 2009


On Sat, 31 Oct 2009, parkbomee wrote:

>
> Thank you both.
>
> However, using tapply() instead of a loop does not seem to improve my code much.
> I am using this inside of an optimization function,
> and it still takes more than it needs...
>


Well, you haven't given us much to work with.

The optimization choices depend on the particulars of your problem, which 
you've not detailed.

It does not take long to run the tapply() code once, so you need to do it 
many times. Right?

If so, you need to say how the structure varies (rendered as DF in David's 
reply) from iteration to iteration in the optimization.

If it turns out that only 'value' changes and that the number of different 
times is not too large, then precomputing suitable indicator matrics may 
help:

  	mat1 <- model.matrix( ~ 0 + factor(time):as.numeric(choice==1),DF)
  	mat2 <- model.matrix( ~ 0 + factor(time), DF )

Inside the optimization use something like

  	with(DF,(value%*%mat1)/(value%*%mat2))

If the structure can change or the number of unique times is large, then 
with so simple a calculation you should probably just inline some C code.

 	http://cran.r-project.org/web/packages/inline/index.html

HTH,

Chuck

>
>
>> CC: bbom419 at hotmail.com; r-help at r-project.org
>> From: dwinsemius at comcast.net
>> To: d.rizopoulos at erasmusmc.nl
>> Subject: Re: [R] avoiding loop
>> Date: Sat, 31 Oct 2009 22:26:17 -0400
>>
>> This is pretty much equivalent:
>>
>> tapply(DF$value[DF$choice==1], DF$time[DF$choice==1], sum) /
>>          tapply(DF$value, DF$time, sum)
>>
>> And both will probably fail if the number of groups with choice==1 is
>> different than the number overall.
>>
>> --
>> David.
>>
>> On Oct 31, 2009, at 5:14 PM, Dimitris Rizopoulos wrote:
>>
>>> one approach is the following:
>>>
>>> # say 'DF' is your data frame, then
>>> with(DF, {
>>>    ind <- choice == 1
>>>    n <- tapply(value[ind], time[ind], sum)
>>>    d <- tapply(value, time, sum)
>>>    n / d
>>> })
>>>
>>>
>>> I hope it helps.
>>>
>>> Best,
>>> Dimitris
>>>
>>>
>>> parkbomee wrote:
>>>> Hi all,
>>>> I am trying to figure out a way to improve my code's efficiency by
>>>> avoiding the use of loop.
>>>> I want to calculate a conditional mean(?) given time.
>>>> For example, from the data below, I want to calculate sum((value|
>>>> choice==1)/sum(value)) across time.
>>>> Is there a way to do it without using a loop?
>>>> time  cum_time  choice    value
>>>> 1         4             1           3
>>>> 1         4              0           2
>>>> 1          4             0           3
>>>> 1          4             0           3
>>>> 2         6             1           4
>>>> 2         6             0           4
>>>> 2         6             0           2
>>>> 2         6             0           4
>>>> 2         6             0           2
>>>> 2         6             0           2 3         4
>>>> 1           2 3         4             0           3 3
>>>> 4             0           5 3         4             0           2
>>>> My code looks like
>>>> objective[1] = value[1] / sum(value[1:cum_time[1])
>>>> for (i in 2:max(time)){
>>>>     objective[i] = value[cum_time[i-1]+1] /
>>>> sum(value[(cum_time[i-1]+1) : cum_time[i])])
>>>> }
>>>> sum(objective)
>>>> Anyone have an idea that I can do this without using a loop??
>>>> Thanks.
>>>>
>>>> _________________________________________________________________
>>>> [[elided Hotmail spam]]
>>>> 	[[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.
>>>
>>> --
>>> Dimitris Rizopoulos
>>> Assistant Professor
>>> Department of Biostatistics
>>> Erasmus University Medical Center
>>>
>>> Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
>>> Tel: +31/(0)10/7043478
>>> Fax: +31/(0)10/7043014
>>>
>>> ______________________________________________
>>> 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.
>>
>> David Winsemius, MD
>> Heritage Laboratories
>> West Hartford, CT
>>
>
> _________________________________________________________________
> [[elided Hotmail spam]]
> 	[[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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901




More information about the R-help mailing list