tapply() function
> tapply(TOTINC, SEX, mean) # finds the mean of Total_Income based on sex
Note: 1= male, 2=female
aggregate() function
Aggregate is similar to tapply, but it is much more flexible. The general structure of aggregate is as follows::
To use aggregate()function in R, make sure to load the stats package:
> library(stats)
The following runs aggregate() with the same parameters as tapply() in previous example:
To subset the data on more than one variable such as "sex" and "pr":