top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

R: Difference between apply vs sapply vs lapply vs tapply?

+3 votes
5,715 views

What are the differences between following functions in R?

posted Feb 11, 2015 by Khusboo

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

apply - When you want to apply a function to the rows or columns or both of a matrix and output is a one-dimensional if only row or column is selected else it is a 2D-matrix
lapply - When you want to apply a function to each element of a list in turn and get a list back.
sapply - When you want to apply a function to each element of a list in turn, but you want a vector back, rather than a list.
tapply - When you want to apply a function to subsets of a vector and the subsets are defined by some other vector, usually a factor.

answer Feb 12, 2015 by Salil Agrawal
...