R-convert transaction format dataset to basket format for Market Basket Analysis

3.2k views Asked by At

First I would like to clarify that before posting this query, I have referred the following links in this site to find answer, but couldn't understand, maybe because they address different problems or because I am new to R.

R-convert transaction format dataset to basket format for sequence mining

Arules Sequence Mining in R

How to handle "argument 'incomparables != FALSE' is not used (yet)"?

I want to do Market Basket Analysis with my dataset. My dataset is in transaction format (as described below) and I want to convert it to Basket format (as described below).

My input file is a csv file with dataset in transaction format as follows:

TransactionID ProductID
A              1
A              2
B              1
C              3
A              4
B              3

I want my output file to be a csv file with Basket format as follows:

1 2 4
1 3
3

where {1,2,4} are products bought in transaction A, {1,3} bought in B and so on.

Can you please tell me the R code to do this? I tried with the following code but it is not working.My input file name is "D01_modified1.csv".

library(arulesSequences)
# Read CSV into R
MyData <- read.csv(file="D01_modified1.csv", header=TRUE, sep=",")
s <- unique(MyData,incomparables = FALSE, fromLast = FALSE,paste0("ProductID"))
# Write CSV in R
write.csv(s, file = "MyOutput.csv",row.names=FALSE, na="")

It is giving the following error :

Error: argument 'incomparables != FALSE' is not used (yet)

Also I am not sure if the following code will give me the desired output or not.

s <- unique(MyData,incomparables = FALSE, fromLast = FALSE,paste0("ProductID"))

Please guide. Looking forward to your help. Thanks a lot...

1

There are 1 answers

0
Claudia Guirao On

It works for me

df_fact <- data.frame(lapply(MyData,as.factor))
df_trans <- as(df_fact, 'transactions')

Hope it helps.