问题
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.
回答1:
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
回答2:
I've had a lot of trouble with coercion (e.g., 'as(dataname, "transactions"..).
I believe that this is due to the fact that I have duplicate records (i.e., the same item purchased more than once in the same transation, when the data is in 'single' format).
This is what finally worked for me:
Transactions<- read.transactions("Data with tx ids, item names, in
single format.csv",
rm.duplicates= TRUE, sep=",",
format = "single", cols = c(7,9));
(tx id in column 7, item names in column 9)
来源:https://stackoverflow.com/questions/32997410/how-to-prep-transaction-data-into-basket-for-arules