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

时光总嘲笑我的痴心妄想 提交于 2019-12-13 04:08:08

问题


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:


It works for me

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

Hope it helps.



来源:https://stackoverflow.com/questions/26946956/r-convert-transaction-format-dataset-to-basket-format-for-market-basket-analysis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!