Use of built-in functions
As a programming language, R comes with low-level operators, such as basic arithmetic operators that can be used to construct more complex operators or functions. While R provides the flexibility to define functions, a performance comparison between an R function versus an equivalent function in a compiled language would almost always favor the latter. However, R and some CRAN packages provide a rich set of functions that are implemented in compiled languages such as C/C++. It is usually preferable to use these functions rather than to write custom R functions to perform the same task.
Consider a simple example of how to calculate the sums of the rows of the following random matrix data
. A code to perform these functions can be constructed by calling the apply()
function, and setting the margin to 1 (representing a row operation) and by setting the FUN
(or function) argument to sum
. Alternatively, R provides a built-in function for this purpose called rowSums
. The...