Create Sparse Matrix from a data frame

时光怂恿深爱的人放手 提交于 2019-11-27 08:40:48

The Matrix package has a constructor made especially for your type of data:

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

Otherwise, you might like knowing about that cool feature of the [ function known as matrix indexing. Your could have tried:

buildUserMovieMatrix <- function(trainingData) {
  UIMatrix <- Matrix(0, nrow = max(trainingData$UserID),
                        ncol = max(trainingData$MovieID), sparse = TRUE);
  UIMatrix[cbind(trainingData$UserID,
                 trainingData$MovieID)] <- trainingData$Rating;
  return(UIMatrix);
}

(but I would definitely recommend the sparseMatrix approach over this.)

This will probably be faster than a loop.

library(reshape2)
m <- dcast(df,UserID~MovieID,fill=0)[-1]
m
#   1 2
# 1 3 0
# 2 0 3
# 3 0 4
# 4 0 3
# 5 0 3

If you use data.tables, it will be a lot faster:

library(data.table)
DT <- as.data.table(df)
m  <- dcast(DT,UserID~MovieID,fill=0)[-1]

And as I'm sure someone will point out, you can use this instead

setDT(df)
m  <- dcast(df,UserID~MovieID,fill=0)[-1]

This converts df to a data.table in place (without making a copy). if your data set is enormous, that can make a difference...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!