关联规则数据格式

时间:2016-09-28 16:21:05

标签: r apriori arules

我正在尝试使用R程序中的apriori算法运行关联规则模型。我有txt文件格式或csv文件格式的数据。我的数据通常看起来像这样。

a, b, c, d,
e, f, g, h,
i, j, k, l, 
(etc.)

数据通常会被读入R

read.transactions("Trial.txt")
transactions in sparse format with
 138 transactions (rows) and
 217 items (columns)

然而,当我尝试运行apriori算法时,我收到错误。

> m1 <- apriori("Trial.txt")
Error in as(data, "transactions") : 
  no method or default for coercing “character” to “transactions”

我认为将数据输入R的方法存在问题。我已尝试删除重复项的方法,但这似乎不起作用。如何将此数据输入R以运行模型?

1 个答案:

答案 0 :(得分:1)

请参阅文档?apriori:第一个参数(data)应该是“类事务的对象或任何可以强制转换为事务的数据结构” 。您提供了长度为1的字符向量,该向量无法强制转换为事务对象。这是一个例子:

writeLines("a,b,c,d
e,f,g,h
i,j,k,l", tf <- tempfile())
library(arules)
(trans <- read.transactions(tf, sep=","))
# transactions in sparse format with
#  3 transactions (rows) and
#  12 items (columns)
m1 <- apriori(trans, parameter = list(confidence = 1, minlen = 4))
head(inspect(m1))
#       lhs    rhs   support confidence lift
# 1 {a,b,c} => {d} 0.3333333          1    3
# 2 {a,b,d} => {c} 0.3333333          1    3
# 3 {a,c,d} => {b} 0.3333333          1    3
# 4 {b,c,d} => {a} 0.3333333          1    3
# 5 {e,f,g} => {h} 0.3333333          1    3
# 6 {e,f,h} => {g} 0.3333333          1    3
相关问题