Apply Functions

tapply

The documentation definition for tapply is a bit more specific than the others, where the arguments are now (X, INDEX, FUN), with X being an object where the split function applies, INDEX is a factor by which X is grouped, and FUN is function as before.

To simplify this definition, we can say tapply applies FUN to X when X is grouped by INDEX.

Examples

In DeathRecords.csv, show the mean Age of death for each Sex.

Click to see solution
library(data.table)
myDF <- fread("/anvil/projects/tdm/data/death_records/DeathRecords.csv")

tapply(myDF$Age, myDF$Sex, mean, na.rm=TRUE)
F
    76.6766847989167
M
    70.2278579695537