在r中解析为事务 [英] Parsing as.transactions in r
问题描述
我一直在努力重写与data.frames一起使用的代码,以便与ffdf一起使用.我有两列,在大惊小怪之后,我设法进行了拆分,并获得了具有以下外观的列表:
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
其中,A,B是购物篮"或分组,而"1 2 3"是特定的分组项目.我现在想要的是将这些转换为事务,并希望能够先验.我已经尝试过简单的
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")
当从data.frame生成数据"时效果很好,但是现在它会产生错误:
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.
推荐答案
应该没问题:
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)
还请注意,您编写的是as(i, "transaction")
而不是as(i, "transactions")
.
Also note that you wrote as(i, "transaction")
and not as(i, "transactions")
.
这篇关于在r中解析为事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!