Thursday, March 14, 2013

Apply-style commands in R

Here's a quick table of what I think are the most useful apply-style commands in R:
FunctionInputOutputBest for
applyRectangularRectangular or vectorApplying function to rows or columns
lapplyAnythingListNon-trivial operations on almost any data type
sapplyAnythingSimplified (if possible) or listSame as lapply, but with simplified output
plyr::ddplydata.framedata.frameApplying function to groupings defined by variables
For alternatives to plyr, see this post on StackOverflow.


  1. The one of this family I use most is tapply. If, say, you want a table of means of a numeric variable x arranged by groups in g, then tapply(x,g,mean) does the job.

    I don't know about plyr, but tapply is easy enough for me.

  2. Is there an easy way to do an apply command inputting multiple (indexable) objects? The function would then receive, for each ith invocation, the ith elements of each input object. The call might look something like multApply(listA, listB, listC, FUN=function(a_item, b_item, c_item){...}). This is a case where I still revert to a loop, but really don't need the loop counter for other purposes. One could re-package the data to use lapply (so that each element of a new list contained an item from A, B, and C), but thats a bit of a pain.

    1. Just found mapply() which does just that. There are plyr versions too.