The apply, lapply and sapply functions in R

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.

apply1

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.

apply2

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s