ARCHIVED: Good practices in R programming
On this page:
- Overview
- Avoid unnecessary operators
- Avoid growing objects inside loops
- Use vectorization if possible
- Get help
Overview
Following are guidelines and code examples that illustrate good practices in R programming. At Indiana University, R is available on the research supercomputers and via IUanyWare.
Avoid unnecessary operators
R is an interpreted language; every operator in your R scripts requires a name lookup every time you use it.
The following two code examples are functionally equivalent. However, the first code example takes about twice as much processing time due to the multiple parentheses.
Example1 | Example2 |
---|---|
user system elapse 0.125 0.000 0.125 |
user system elapse 0.055 0.000 0.055 |
Avoid growing objects inside loops
Always pre-allocate objects to be used inside loops. Executing loops in R is slow, and growing objects inside loops will make your R program particularly slow. You should always try to pre-allocate vectors, lists, and data frames accessed inside any loops.
Consider the following two code examples. The first accesses and grows a vector inside the for loop while the second pre-allocates the vector and accesses the vector inside the for loop without growing its size.
Example1 | Example2 |
---|---|
user system elapse 0.257 0.000 0.257 |
user system elapse 0.099 0.000 0.099 |
Use vectorization if possible
In R, everything is a vector. In your R script, you should always write vectorized code or use pre-existing compiled kernels (which are already vectorized and optimized) to avoid interpreter overhead.
Consider the following two code examples. The second example achieves a 38-fold speedup by using vectorized code provided by compiled kernels.
Example1 | Example2 |
---|---|
user system elapse 0.348 0.000 0.348 |
user system elapse 0.009 0.000 0.009 |
Get help
For help with R on the IU research supercomputers, contact the UITS Research Applications and Deep Learning team.
This is document aaxp in the Knowledge Base.
Last modified on 2024-02-16 12:41:26.