In R I would like to create a transactions data with the following data frame so I can run apriori
in package arules
. It has transaction IDs, item IDs and category IDs, parents of items.
Transaction_ID Item_ID Category_ID
T01 A001 A01
T01 A002 A01
T02 A001 A01
T02 A003 A02
T02 A002 A01
T03 A005 A03
T05 A004 A03
T05 A002 A01
T05 A005 A03
T04 A001 A01
T04 A003 A02
I would like to incoporate category IDs as a level above labels (items) into the transactions data as Groceries
data.
str(Groceries)
Formal class 'transactions' [package "arules"] with 3 slots
..@ data :Formal class 'ngCMatrix' [package "Matrix"] with 5 slots
.. .. ..@ i : int [1:43367] 13 60 69 78 14 29 98 24 15 29 ...
.. .. ..@ p : int [1:9836] 0 4 7 8 12 16 21 22 27 28 ...
.. .. ..@ Dim : int [1:2] 169 9835
.. .. ..@ Dimnames:List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : NULL
.. .. ..@ factors : list()
..@ itemInfo :'data.frame': 169 obs. of 3 variables:
.. ..$ labels: chr [1:169] "frankfurter" "sausage" "liver loaf" "ham" ...
.. ..$ level2: Factor w/ 55 levels "baby food","bags",..: 44 44 44 44 44 44 44 42 42 41 ...
.. ..$ level1: Factor w/ 10 levels "canned food",..: 6 6 6 6 6 6 6 6 6 6 ...
..@ itemsetInfo:'data.frame': 0 obs. of 0 variables
However, read.transactions
let you import transaction ID and item ID only with parameter cols. I have also tried this
transaction_by_item<-split(df[,c("Item_ID","Category_ID")],df$Transaction_ID)
basket <- as(transaction_by_item, "transactions")
and it gave an error
Error in asMethod(object) : can coerce list with atomic components only
It works if I just try to split transactions with item IDs only. transaction_by_item<-split(df$Item_ID,df$Transaction_ID)
Anyone knows how to incorporate both item IDs (labels) and category IDs (level) when creating a transaction data? Thanks.