从数据框创建稀疏矩阵

4
I'm trying to create a sparse data matrix from a data frame without building a dense matrix, which can cause serious memory issues. I found a solution in the following post on SO: Create Sparse Matrix from a data frame. I tried this solution, but it didn't work for me, possibly because my UserID and MovieID don't start at 1. Here's my sample code:
library(Matrix)

UserID<-c(10090,10090,10090,10316,10316)
MovieID <-c(63155,63530,63544,63155,63545)
Rating <-c(2,2,1,2,1)
trainingData<-data.frame(UserID,MovieID,Rating)
trainingData

UIMatrix <- sparseMatrix(i = trainingData$UserID,
                         j = trainingData$MovieID,
                         x = trainingData$Rating)

dim(UIMatrix)

我希望得到一个 2 x 3 的矩阵,但是维度对应于最大的用户和电影 ID。

我尝试了帖子中提出的第二个解决方案,但它在我的数据上不起作用。

有人可以给些建议吗?


1
如果我理解i, j是行/列索引。因此,trainingData$UserID具有行索引10090,..,列索引也很大。因此,矩阵大小足够大,可以拥有这些行/列索引。 - akrun
1个回答

1
你可以使用 as.integer(as.factor(.)) 将索引转换为从一开始的索引。
UIMatrix <- sparseMatrix(i = as.integer(as.factor(trainingData$UserID)),
                         j = as.integer(as.factor(trainingData$MovieID)),
                         x = trainingData$Rating)

dim(UIMatrix)
# [1] 2 4

dimnames(UIMatrix) <- list(sort(unique(trainingData$UserID)),
                           sort(unique(trainingData$MovieID)))

UIMatrix
# 2 x 4 sparse Matrix of class "dgCMatrix"
#       63155 63530 63544 63545
# 10090     2     2     1     .
# 10316     2     .     .     1

几乎完美!我只需要添加dimnames。我该怎么做? - Nelson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接