Sparse matrix in R based on the data frame Suppose I have book ratings in the form of data frame (where 0 means no rating):

$\begin{array}{|c|c|c|}
\hline
\textbf{User.ID}&amp; \textbf{ISBN} &amp; \textbf{Book.Rating} \\ \hline
 276725 &amp; 034545104X &amp; 0 \\ \hline
 276726 &amp; 0155061224 &amp; 5 \\ \hline
 276725 &amp; 3257224281 &amp; 7 \\ \hline
 ... &amp; ... &amp; ... \\ \hline
\end{array}$

In what easiest way can I get the form as below (I want to use it to create a realRatingMatrix object) ?

$\begin{array}{|c|c|c|c|c|}
\hline
\ &amp; \textbf{034545104X} &amp; \textbf{0155061224} &amp; \textbf{3257224281} &amp; ...\\ \hline
 \textbf{276725} &amp; . &amp; 3 &amp; 7 &amp; ...\\ \hline
 \textbf{276726} &amp; 5 &amp; 5 &amp; . &amp; ...\\ \hline
 ... &amp; ... &amp; ... &amp; ... &amp; ...\\ \hline
\end{array}$
Your data looks like this at the moment:

data &lt;- data.frame(User_Id = c(276725, 276726, 276725, 276726, 276725),
                   ISBN = c("A", "B", "C", "A", "B"),
                   Book_Rating = c(0, 5, 7, 5, 3))

&gt; data

  User_Id ISBN Book_Rating
1  276725    A           0
2  276726    B           5
3  276725    C           7
4  276726    A           5
5  276725    B           3


With the following commands you can create a dgCMatrix, required as input for a realRatingMatrix object.

library(Matrix)
data_sparse = sparseMatrix(as.integer(data$User_Id), as.integer(data$ISBN), x = data$Book_Rating)
colnames(data_sparse) = levels(data$ISBN)
rownames(data_sparse) = levels(data$User_Id)

&gt; data_sparse

276726 x 3 sparse Matrix of class "dgCMatrix"
     A B C
[1,] . . .
[2,] . . .
[3,] . . .

..............................
........suppressing rows in show(); maybe adjust 'options(max.print= *, width = *)'
..............................

[276722,] . . .
[276723,] . . .
[276724,] . . .
[276725,] 0 3 7
[276726,] 5 5 .


After that you can call the command

new("realRatingMatrix", data = data_sparse)


Bear in mind that this matrix starts at one and it's only populated in two rows (276725 and 276726) but the rest of the columns from 1 to 276725 exist. If you don't want to use User_Id as indices you will have to create new indices and use those instead and have one User_Id correspond to a new index. That means if User_Id starts at, for instance, [11, 18...] then it'd become User_Id 11 = New_Index 1, User_Id 18 = New_Index = 2. Use New_Index instead of User_Id and you're done.

If that's the case then data should end up looking like this:

data &lt;- data.frame(User_Id = as.factor(c(276725, 276726, 276725, 276726, 276725)),                       
                   ISBN = c("A", "B", "C", "A", "B"),
                   Book_Rating = c(0, 5, 7, 5, 3),
                   New_Index = c(1, 2, 1, 2, 1))

&gt; data

  User_Id ISBN Book_Rating New_Index
1  276725    A           0         1
2  276726    B           5         2
3  276725    C           7         1
4  276726    A           5         2
5  276725    B           3         1

library(Matrix)
data_sparse = sparseMatrix(as.integer(data$New_Index), as.integer(data$ISBN), x = data$Book_Rating)
colnames(data_sparse) = levels(data$ISBN)
rownames(data_sparse) = levels(data$User_Id)

&gt; data_sparse

2 x 3 sparse Matrix of class "dgCMatrix"
       A B C
276725 0 3 7
276726 5 5 .