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
Using the 5000_transactions csv file, find the sum of the amount spent (in the SPEND column) at each of the store regions (the STORE_R column)
Click to see solution
# read in data
library(data.table)
myDF <- fread("/anvil/projects/tdm/data/8451/The_Complete_Journey_2_Master/5000_transactions.csv")
tapply(myDF$SPEND, myDF$STORE_R, sum, na.rm=TRUE)
CENTRAL
8897305.13999992
EAST
11699446.8599998
SOUTH
7957920.76999994
WEST
9680106.5399999