2

I am experimenting with the apriori algorithm in the arules package.

This is what I've done: I loaded a view from SQL Server into R. Since that data is not in transactions form (to use in apriori), I had to convert it:

data <- sapply(orders, as.factor)

Then I entered the apriori function:

apriori(data, parameter = list (support=0.005, confidence=0.5))

I get this error:

Error in t(as(from, "ngCMatrix")) : error in evaluating the argument 'x' in selecting a method for function 't': Error in asMethod(object) : cannot coerce 'NA's to "nsparseMatrix"

I checked with a query and I don't even have any attribute that is NULL/NA.

I don't understand what the error means. Does someone know what the problem is and how to solve this?

4

2 回答 2

2

我最近遇到了同样的错误。我所学到的只是你的数据必须被强制用于挖掘项目集或规则的事务。这段代码应该会有所帮助。

transaction_data<- as(data, "transactions")
rules <- apriori(transaction_data,parameter = list(minlen=2,supp=0.2,conf=0.5))
于 2015-01-22T03:30:09.167 回答
2

当您尝试使用R分组功能时,就会出现主要问题。正如您在此处看到的那样,大多数分组函数不会返回一个data.frame. 在您的情况下,您使用sapply了它返回一个向量。确保您处理适当的转换:

data = data.frame(sapply(orders,as.factor))

然后按照关联规则构建:

apriori(data, parameter = list (support=0.005, confidence=0.5))

这可以按预期工作(经过测试)。

于 2015-10-25T13:26:21.663 回答