0
votes

I am trying to run an association rule model using the apriori algorithm in the R program. I have my data in either txt file format or in csv file format. My data generally looks like this.

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

The data is usually read into R

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

However when I try to run the apriori algorithm, I get an error.

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

I think there is a problem with how I am entering the data into R. I have tried the methods for removing duplicates and this does not appear to work. How should I enter this data into R to run the model?

1

1 Answers

1
votes

See the documentation ?apriori: the first argument (data) is expected to be an "object of class transactions or any data structure which can be coerced into transactions". You provided a character vector of length 1, which cannot be coerced into a transactions object. Here's an example:

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