Brief Information

tapply() is a function from base package.

t in tapply() stands for table or tabulating the result/output into groups.

tapply() applies function to subset of a column, and that subset is based on levels of a factor.

You can also say tapply() is for group wise computation and the groups are made based on the levels of a factor column.

tapply() on a single column

For example in iris dataset, If I want mean of Sepal.Length column, Specieslevel wise (factor column). Here is how I would do.

tapply(iris$Sepal.Length, iris$Species, mean)
    setosa versicolor  virginica 
     5.006      5.936      6.588 

tapply() on multiple columns

If I want mean of each nuemric column based on factor column levels, I can combine lapply() and tapply() together like below

lapply(iris[,1:4], function(x) tapply(x,iris$Species, mean))

cat("\n\n\n")
writeLines("In data.frame form")
data.frame(lapply(iris[,1:4], function(x) tapply(x,iris$Species, mean)))
$Sepal.Length
    setosa versicolor  virginica 
     5.006      5.936      6.588 

$Sepal.Width
    setosa versicolor  virginica 
     3.428      2.770      2.974 

$Petal.Length
    setosa versicolor  virginica 
     1.462      4.260      5.552 

$Petal.Width
    setosa versicolor  virginica 
     0.246      1.326      2.026 




In data.frame form
           Sepal.Length Sepal.Width Petal.Length Petal.Width
setosa            5.006       3.428        1.462       0.246
versicolor        5.936       2.770        4.260       1.326
virginica         6.588       2.974        5.552       2.026