Sparse matrix in R based on the data frame
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 .