Centar 15 Centar 15 - 1 year ago 135
R Question

Parsing as.transactions in r

I've been working on rewriting my code that worked with data.frames to work with ffdf. I had two columns, and after a lot of fuss I've managed to do a split and get a list with the following look:

data=
$A
1 2 3
$B
4 5 6


where A,B are the "baskets" or groupings, and "1 2 3" specific grouped items. What I want now is to convert these to transactions and hopefully manage to do an apriori. I've tried the simple

as(i, "transaction")


which worked well when "data" was generated from a data.frame but now it produces an error:

Error in as(data, "transactions") :
no method or default for coercing “list” to “transactions”


I've seen that duplicate items can cause these problems, so I've eliminated those but the error remains.

Answer Source

Should be no problem:

library(arules)
data <- list(A=1:3, B=4:6)

showMethods("coerce", classes="transactions")
# Function: coerce (package methods)
# from="data.frame", to="transactions"
# from="list", to="transactions"
# from="matrix", to="transactions"
# from="ngCMatrix", to="transactions"
# from="tidLists", to="transactions"
# from="transactions", to="data.frame"
# from="transactions", to="list"
# from="transactions", to="matrix"
# from="transactions", to="tidLists"

class(data)
# [1] "list"
as(data, "transactions")
# transactions in sparse format with
#  2 transactions (rows) and
#  6 items (columns)

Also note that you wrote as(i, "transaction") and not as(i, "transactions").