[R] “For” calculation is so slow

Liviu Andronic landronimirc at gmail.com
Tue May 22 11:01:37 CEST 2012


On Tue, May 22, 2012 at 9:01 AM, jiangxijixzy <jiangxijixzy at 163.com> wrote:
> The function I wrote can run well with the small data, but with the large
> data, the function runs very very slowly. How can I correct it? Thank you
> very much. My function as below:
>
I guess this is a classic loops vs vectorization problem.

> fortune('treat')

Contrary to popular belief the speed of R's interpreter is rarely the limiting
factor to R's speed. People treating R like C is typically the limiting factor.
You have vector operations, USE THEM.
   -- Byron Ellis
      R-help (October 2005)

I would suggest that you read up on vectorization in R to understand
why it is often preferred to loops. Two obvious places are an Rnews
article by John Fox (if I remember well) and his Companion to Applied
Regression book.

Moreover, please sign your messages with your real name. Regards
Liviu



More information about the R-help mailing list