by(data, INDICES, FUN, …)
Apply a function across subsets of data, where the subsets are defined by the INDICES argument. The by function returns a list, where each list item represents the results for a particular subset of the data.
  • data – The full set of data, e.g. a vector or data.frame.
  • INDICES – A vector describing how the data subsets are to be constructed (e.g. a factor vector). Multiple columns in a data frame may also be given, where each combination of the variables defines a new subset of data.
  • FUN – A function to apply to each subset of data.
  • – Any additional arguments to pass to FUN.

Example. The textbooks data from the openintro package is loaded. Then we examine a summary of the price differences between new textbook prices at the UCLA bookstore and on Amazon for two subsets: courses where one book is required and courses where more than one book is required.
> library(openintro)
Loading required package: Cairo
Please visit for free statistics

Attaching package: ‘openintro’

The following object(s) are masked from ‘package:datasets’:


> data(textbooks)
> by(textbooks$diff, textbooks$more, summary)
textbooks$more: N
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 -0.250   9.262  14.530  17.460  28.260  40.940 
textbooks$more: Y
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 -9.530   1.750   4.960   9.836   9.480  66.000 
It seems college students are wise to shop online!

Leave a Reply