【问题标题】:Cannot convert dataframe to transactions object无法将数据框转换为事务对象
【发布时间】:2013-04-10 10:14:16
【问题描述】:

我想使用arules 包来练习用R 挖掘关联规则。 数据是

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"))

通过做

table5 <- as(datt, "transactions")

出现此错误:

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

如何将我的对象转换为“事务”对象?

【问题讨论】:

    标签: r arules


    【解决方案1】:

    也许你忘记加载arules 包。

    library(arules)
    

    【讨论】:

      【解决方案2】:

      试试这个:

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

      这里的错误很明显:

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

      class(datt)cast_df,并且没有这种类型的强制方法 (as)。

      请注意,当您使用arules 包时,通常不需要手动进行强制转换,该函数会尝试在内部进行正确的强制转换。例如:

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

      【讨论】:

      • 感谢您的帮助。但是在我的电脑中,您的方式有误。错误如下:ERROE:as(as.matrix(datt), "transactions") : no method or default将“cast_matrix”强制转换为“transactions”
      【解决方案3】:

      对我来说这很有效

      install.packages("arules")
      

      【讨论】:

        【解决方案4】:

        我遇到了同样的错误,通过添加库(矩阵)修复了它

        希望对你有帮助

        【讨论】:

        • 这可以是评论而不是答案。请阅读规则。
        【解决方案5】:

        这些问题与在 markdown 中的代码块之前加载库有关。您已在 R 环境中加载了该库,因此代码可以正常工作。但是对于针织,你需要在markdown中加载库。

        【讨论】:

          猜你喜欢
          • 2015-10-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多