Applying families of functions 

The apply family of functions allow users to easily apply custom vector functions on matrices, lists, and other R data structures. Operations such as summing by rows and columns or more complex ones, such as functions with conditional logic, can be applied using apply commands. These are extremely convenient, not only from an ease-of-use perspective but also from a performance standpoint. In general, vectorised operations will almost always be faster and more efficient than looping, such as with for-next loops in R:

# apply # Usage # # apply(X, MARGIN, FUN, ...) # Arguments # # X: an array, including a matrix. # MARGIN: a vector giving the subscripts which the function will be applied over. E.g., for a matrix ...

Get Hands-On Data Science with R now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.