Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

r - How to prep transaction data into basket for arules

Ok, so I have searched a lot and want to run arules on sales data. I just need to properly get the data in the right format and set up with the correct "factors" or "variables" and in basket form.

Right now I have sales data with the Order# and then the items inside that. Each order is unique (every new order, a new # gets created and includes the part#), but the same items obviously can appear in many orders.

Currently, my data is set up like this:

Order#    Part#   PartDescription
1         A       PartA
1         B       PartB
1         G       PartG
2         R       PartR
3         A       PartA
3         B       PartB
4         E       PartE
5         Y       PartY
6         A       PartA
6         B       PartB
6         F       PartF
6         V       PartV

So, R doesn't like it in this form, and I have to get it in the form that arules and data analysis will accept.

Yes I save it as a text file and have tried a .csv file, but if I can get step by step instructions on how to prep it or manipulate it in RStudio that'd be great.

I read that it's suppose to be in a basket form such as..

1 (A, B, G)
2 (R)
3 (A, B)
4 (E)
5 (Y)
6 (A, B, F, V)

If that's not accurate please correct me. I get the idea but I just need step by step instructions which I can't seem to find anywhere. I've tried using dplyr and tidyr. I have a good understanding of data analysis but need more direct help on RStudio, so if I could just have that step by step I will understand this further.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Take a look at the help page for the "transactions" data type for examples on how to get your data in:

library(arules)
?transactions

For your type, you want to split by Order, then use as to get it into a transactions list:

trans <- as(split(data[,"Part"], data[,"Order"]), "transactions")
inspect(trans)
  items     transactionID
1 {A,B,G}   1            
2 {R}       2            
3 {A,B}     3            
4 {E}       4            
5 {Y}       5            
6 {A,B,F,V} 6   

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...