Cannot convert dataframe to transactions object

◇◆丶佛笑我妖孽 提交于 2019-12-25 07:26:42

问题


I want to use the arules package to practice mining association rules with R. The data is

datt <- structure(list(Item1 = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 
0L), Item2 = c(0L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L), Item3 = c(0L, 
1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 0L), Item4 = c(0L, 0L, 0L, 1L, 
0L, 0L, 0L, 0L, 0L, 0L), Item5 = c(1L, 0L, 0L, 1L, 0L, 0L, 0L, 
0L, 0L, 1L), Item6 = c(0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L
), Item7 = c(0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L), Item8 = c(0L, 
1L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L), Item9 = c(0L, 1L, 1L, 1L, 
0L, 0L, 0L, 0L, 1L, 0L), Item10 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L)), .Names = c("Item1", "Item2", "Item3", "Item4", 
"Item5", "Item6", "Item7", "Item8", "Item9", "Item10"), row.names = c(2L, 
3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L), class = c("cast_df", 
"data.frame"))

By doing

table5 <- as(datt, "transactions")

this error turns up:

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

What can I do to convert my object into a 'transactions' object?


回答1:


Try this :

as(as.matrix(datt), "transactions")
transactions in sparse format with
 10 transactions (rows) and
 10 items (columns)

The error is explicit here :

no method or default for coercing “cast_df” to “transactions”

class(datt) is cast_df and no coercing method(as) for this type.

Note that generally you don't need to do the coercion manually when you use the arules package, the function will try to do the right coercion internally. For example :

dissimilarity(x=as.matrix(datt),method='cosine') ## works
dissimilarity(x=datt,method='cosine')            ## you get the same coercion error



回答2:


for me this works

install.packages("arules")



回答3:


I got the same error and it was fixed by adding library(Matrix)

Hope that helps



来源:https://stackoverflow.com/questions/15923014/cannot-convert-dataframe-to-transactions-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!