[R] avoiding loop

jim holtman jholtman at gmail.com
Sun Nov 1 20:35:41 CET 2009


What you need to do is to understand how to use Rprof so that you can
determine where the time is being spent.  It probably indicates that
this is not the source of slowness in your optimization function.  How
much time are we talking about?  You may spent more time trying to
optimize the function than just running the current version even if it
is "slow" (slow is a relative term and does not hold much meaning
without some context round it).

On Sat, Oct 31, 2009 at 11:36 PM, parkbomee <bbom419 at hotmail.com> 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...
>
>
>
>> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list