tapply first partitions the elements of elems according to the indices in group_index, then applies function fun on each group's elements.
The following example defines items with that each have a color and a size. With tapply, the average (mean) size for each group is calculated and returned:
In SQL, this example would roughly correspond to the following statement. (Never mind the fact that SQL apparently does not define a mean aggregate function).