Examine which commands are taking up the most time in an R script or function. I’d call this a crapshoot since Rprof doesn’t typically indicate a particular line of code that is taking a disproportionate amount of time. However, Rprof is easy and quick to implement, so it may serve as a nice first attempt to identify what is making code run slowly.
- filename – A file name where to store results.
append(x, values, after=length(x))
Append values to x, possibly inserted into the middle of x. This function is important since it tends to perform a little faster than using the concatenation (c) function.
- x – A vector.
- y – Another vector to place at the end of or within x.
- after – Where values should go in x, by default at the end.
ifelse(test, yes, no)
The ifelse function is used to assign one object or another depending on whether the first argument, test, is TRUE or FALSE. It even works as one would hope when test is a vector.
- test – A logical expression, which may be a vector.
- yes – What to return if test is TRUE.
- no – What to return if test is FALSE.
colSums(x), rowSums(x), colMeans(x), rowMeans(x)
The function apply
was described yesterday. The four functions for today’s post provide even faster ways to perform sums and means across rows and columns.
apply(x, margin, function, …)
apply implements a function across the rows or columns of a matrix. It also works on multidimensional arrays and can be used to apply functions across one or more dimensions of those arrays.
- x – A matrix or array.
- margin – The dimension of the matrix or array x to preserve. If x is an array, then this may be a vector indicating each dimension that should be preserved.
- function – A function (e.g. mean) to be applied across each dimension not listed in margin. In a matrix, the function would be applied across the rows or columns of x, whichever dimension is not listed in the margin argument.
Save a particular set of objects using save, or save all objects in the current R session via save.image. Load an R data file into a session using load.
- … – List all objects that should be saved.
- file – Provide the file name, typically ending in .rda. Also, because the list of objects to save is arbitrarily long, the file argument must begin with the argument name, i.e. include file= in this argument’s declaration.
- file – Provide the file name, typically ending in .rda or .RData.
- file – The name of the file to be loaded.
In each case, the file name may also include a path to the file.
Use system.time to time how fast R processes an expression. This function is especially useful for comparing the speed of two coding options.
- expr – An R expression to evaluate.