Skip to content
Prev 44380 / 398506 Next

How to write efficient R code

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: