recommenderlab,asMethod(object)中的错误:dup_mMatrix_as_geMatrix的无效类'NA'

时间:2013-09-02 20:41:25

标签: r recommendation-engine

我正在尝试将矩阵更改为可以在recommenderlab包的函数中使用的结构。

datafile1 <- as(datafile1,"matrix")
datafile1
     name1 name2 rating1 rating2 rating3 rating4 rating5 rating6
[1,] "1"   "a"   "0"     "0"     "1"     "0"     "0"     "0"    
[2,] "2"   "d"   "0"     "0"     "1"     "0"     "0"     "0"    
[3,] "3"   "x"   "1"     "0"     "1"     "0"     "0"     "0"    
[4,] "4"   "b"   "0"     "1"     "1"     "0"     "0"     "0"  

library(recommenderlab)
datafile1 <- as(datafile1, "realRatingMatrix")

结果如下:

  

asMethod(object)出错:对dup_mMatrix_as_geMatrix无效的类'NA'

有没有人知道这里出了什么问题?

1 个答案:

答案 0 :(得分:15)

问题是RealRatingMatrix类扩展了MatrixMatrix没有实现其中包含character的矩阵。首先将您的matrix转换为numeric,然后转换。

# Recreate data
datafile1<-read.table(textConnection('
name1 name2 rating1 rating2 rating3 rating4 rating5 rating6
"1"   "a"   "0"     "0"     "1"     "0"     "0"     "0"    
"2"   "d"   "0"     "0"     "1"     "0"     "0"     "0"    
"3"   "x"   "1"     "0"     "1"     "0"     "0"     "0"    
"4"   "b"   "0"     "1"     "1"     "0"     "0"     "0"  
'),header=TRUE)
datafile1<-as.matrix(datafile1)

# Convert to numeric (by arbitrarily map the characters to numbers.)
datafile1<-sapply(data.frame(datafile1),as.numeric)

# Create real rating matrix
as(datafile1, "realRatingMatrix")
# 4 x 8 rating matrix of class ‘realRatingMatrix’ with 32 ratings.