NewbieToR
NewbieToR

Reputation: 1

Shaping dataset for apriori (market basket) analysis in R

I have a dataset that looks like this:

Product  Customer  Visit_Time
1       140       Jan
2       140       Jan
1       184       Jan
4       140       Feb
8       192       Mar

If I split the data by split(product, customer), I end up getting all the visits together (february purchases end up together with january purchases).

How can I add a column to make it look like this

OrderId  Product  Customer  Visit_Time
1          1       140       Jan
1          2       140       Jan
2          1       184       Jan
3          4       140       Feb
4          8       192       Mar

Upvotes: 0

Views: 353

Answers (2)

NewbieToR
NewbieToR

Reputation: 1

I realized that I can create a unique order ID column by pasting together the variables of interest like this:

dat <- transform(dat, order_id = paste0(Customer, Visit_Time, transactionID))

Upvotes: 0

Michael Hahsler
Michael Hahsler

Reputation: 3075

The easy way to create transactions from your data is:

> dat 
  Product Customer Visit_Time
1       1      140        Jan
2       2      140        Jan
3       1      184        Jan
4       4      140        Feb
5       8      192        Mar

> agg <- aggregate(Product ~ Customer + Visit_Time, data = dat, FUN = c)
> agg
  Customer Visit_Time Product
1      140        Feb       4
2      140        Jan    1, 2
3      184        Jan       1
4      192        Mar       8

> trans <- as(agg$Product, "transactions")
> transactionInfo(trans) <- agg[1:2]
> transactionInfo(trans)$transactionID <- 1:length(trans)

> inspect(trans)
  items Customer Visit_Time transactionID
1 {4}   140      Feb        1            
2 {1,2} 140      Jan        2            
3 {1}   184      Jan        3            
4 {8}   192      Mar        4       

Upvotes: 1

Related Questions