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。
我尝试了帖子中提出的第二个解决方案,但它在我的数据上不起作用。
有人可以给些建议吗?
i, j
是行/列索引。因此,trainingData$UserID具有行索引10090,..
,列索引也很大。因此,矩阵大小足够大,可以拥有这些行/列索引。 - akrun