For a while now I’ve been aware of the apply functions in R, but have preferred to loop because of my unfamiliarity with them. This post is an attempt to get myself up to speed with these functions which appear to be significant time savers (and perhaps less computationally burdensome). The apply function allows you to evaluate a function over the rows or columns of an array in one line of code. For example, suppose you need to know how many NA values are in each column and row of your data set. The following call of the apply function delivers the desired result.
Specifying 2 as the second argument in the apply function executes across columns. Specifying 1 executes the function across the rows. The function is.na returns a logical TRUE/FALSE vector where TRUE indicates a value of NA. The function sum when operating on a logical vector adds up all TRUE values. If the function is already defined in R, one can replace function(x) … with the function name and argument x.
The lapply and sapply functions execute along a vector or list. lapply returns a list and sapply returns a vector. Suppose you want to square every element of a list. Squaring the list as if you would square every element of a vector will give an error message. The following lapply and sapply calls deliver the desired result.
And thats it. Relatively easy to learn and much simpler than running a for loop every time you need to conduct operations on rows/columns of a matrix or a list.