[R] How to write efficient R code

Tom Blackwell tblackw at umich.edu
Tue Feb 17 18:21:42 CET 2004

Lennart  -

My two rules are:

  1. Be straightforward.  Don't try to be too fancy.  Don't worry
	about execution time until you have the WHOLE thing programmed
	and DOING everything you want it to.  Then profile it, if it's
	really going to be run more than 1000 times.  Execution time
	is NOT the issue.  Code maintainability IS.

  2. Use vector operations wherever possible.  Avoid explicit loops.
	However, the admonition to avoid loops is probably much less
	important now than it was with the Splus of 10 or 15 years ago.

(Not that I succeed in obeying these rules myself, all the time.)

Remember:  execution time is not the issue.  memory size may be.
clear, maintainable code definitely is.

In my opinion, the occasional questions you will see on this list about
incorporating C code, or trying to specify one data type over another,
come up only in very unusual, special cases.  Almost everything can be
done without loops in straight R, if you think about it first.

-  tom blackwell  -  u michigan medical school  -  ann arbor  -

On Tue, 17 Feb 2004 Lennart.Borgman at astrazeneca.com wrote:

> I have been lurking in this list a while and searching in the archives to
> find out how one learns to write fast R code. One solution seems to be to
> write part of the code not in R but in C. However after finding a benchmark
> article (http://www.sciviews.org/other/benchmark.htm) I have been more
> interested in making the R code itself more efficient. I would like to find
> more info about this. I have tried to mail the contact person for the
> benchmark, but I have so recieved no reply.
> I am not an R programmer (or statistican) so I do not know R well. I am
> looking for some advice about writing fast R code. What about the different
> data types for example? Is there some good place to start to look for more
> info about this?
> Thanks for any pointers
> Lennart
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

More information about the R-help mailing list