【问题标题】:Conversion of dataframe into a realRatingMatrix in R将数据帧转换为 R 中的 realRatingMatrix
【发布时间】:2017-03-15 08:18:52
【问题描述】:

我正在尝试转换包含三列 username、itemnumber 或 itemID 和评级的数据框。下面提到了三列的数据类型。

>             userID          itemid         rating
               <chr>           <int>          <dbl>

当所有列都是数字时,我找到了将数据框转换为 realRatingMatrix 的方法。但是,我找不到一个函数来转换这个包含字符和数字数据类型的数据帧。 有人可以建议任何将数据框转换为 realRatingMatrix 的解决方案。

【问题讨论】:

  • 你是如何将数据框转换成realReatingMatrix的?
  • 数据帧的转换数据类型为 int 的所有变量都可以转换为 realRatingMatrix 是一个两步过程。首先,使用 as.matrix() 将数据帧转换为矩阵。其次,使用 as(m, "realRatingMatrix) 函数将矩阵转换为 realRatingMatrix。

标签: r dataframe recommendation-engine


【解决方案1】:

as(data.frame,"realRatingMatrix")

【讨论】:

    【解决方案2】:

    几个步骤,这对我有用:

    m$userID <- as.factor(m$userID)
    m$itemID <- as.factor(m$itemID)
    m$rating <- as.numeric(m$rating)
    
    m <- as(m, "realRatingMatrix")
    

    我需要我的评分是数字,不确定 double 是否有效,但我看不出它不应该的原因。

    【讨论】:

      【解决方案3】:

      我可能迟到了,但我找到了解决办法。无需将数据框转换为 realRatingMatrix,而是将 as 函数的返回表达式分配给临时变量。

      所以只需一个temp &lt;- as(data.frame,"realRatingMatrix") 就足够了。 class(temp) 会给你输出 - [1] "realRatingMatrix"。无需将 char 字段转换为因子。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-08
        • 2014-04-15
        • 1970-01-01
        相关资源
        最近更新 更多