Re: [R] finding a faster way to do an iterative computation

From: dxc13 <dxc13_at_health.state.ny.us>
Date: Tue, 29 Jul 2008 16:34:32 -0700 (PDT)

Thank you to all who applied. These all seem to work the way I want them to. The outer function seems really useful, I probably could use that for a lot of my work.
Thanks!

Rolf Turner-3 wrote:
>
>
> On 30/07/2008, at 6:12 AM, dxc13 wrote:
>

>>
>> useR's,
>>
>> I am trying trying to find out if there is a faster way to do a  
>> certain
>> computation.  I have successfully used FOR loops and the apply  
>> function to
>> do this, but it can take some time to fully compute, but I was  
>> wondering if
>> anyone may know of a different function or way to do this:
>>> x
>> [1] 1 2 3 4 5
>>> xk
>>  [1] 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0
>>
>> I want to do: abs(x-xk[i]) where i = 1 to length(xk)=13.  It should  
>> result
>> in a 13 by 5 matrix or data frame.  Does anyone have a "quicker"  
>> solution
>> than FOR loops or apply()?

>
> outer(xk,x,function(a,b){abs(a-b)})
>
> ######################################################################
> Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
>
> ______________________________________________
> 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.
>
>
-- 
View this message in context: http://www.nabble.com/finding-a-faster-way-to-do-an-iterative-computation-tp18718233p18723369.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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 Wed 30 Jul 2008 - 01:06:05 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 Wed 30 Jul 2008 - 01:33:09 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