Skip to Main Content

Learn R

This guide focuses on transformation and cleaning functions in R that are especially useful for working with tabular datasets.

Tapply and Aggregate Functions in R

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":

 

 

 

 

 

 

 

 

 

 

 

 

Liaison Librarian

Profile Photo
Martin Morris
Contact:
Schulich Library of Physical Sciences, Life Sciences and Engineering
Macdonald-Stewart Library Building
809 rue Sherbrooke Ouest
Montréal, Québec H3A 0C1
(514) 398 8140
Website Skype Contact: martinatmcgill
Social: Twitter Page

McGill LibraryQuestions? Ask us!
Privacy notice