Association Rules in R

Market Basket Analysis (MBA) or Affinity Analysis or Association Analysis is an analysis to understand combination and sequence of activities.  A simple non-technical description of Market Basket Analysis (MBA), you are advised to read blog - Retail Market Basket Analysis.

Detailed steps required for Market Basket Analysis are explained in this blog - Market Basket Analysis using R

Main objective of the blog is to prepare data for running association rules using R. Package arules provide required functions. We have created a hypothetical example and data for showing steps to transform data for running association rule. Data frame has information on quantities bought on each order at a retail store. Example data is in the below format.


Since, we want to find association among products, we can ignore the quantities of product bought together. If you are keen to consider quantities, one of the option is to create duplicates based on combinations if that is going to be tricky. Transform data for Association or Market Basket Analysis and find combinations of the products bought together in an order. We can use aggregate and paste for getting output in the required format.


Install and load arules package for running association rules or Market Basket Analysis (MBA).


apriori function  requires class of input data to be transactions ; hence We need to convert list to transactions before using association algorithm. Now change the class of object order.level to transactions.


Run apriori function to find associations or association rules between products bought together.





 Considering, size of data , not useful insights can be generated in this example. We have written  a detailed blog on Market Basket Analysis (MBA) using R.

Leave a Comment