Skip to main content
new improvement without bug
Source Link
wacax
  • 3.5k
  • 4
  • 26
  • 48
data <- 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))

> 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$New_Indexdata$User_Id)

> data_sparse

2 x 3 sparse Matrix of class "dgCMatrix"
       A B C
[1,]276725 0 3 7
[2,]276726 5 5 .
data <- 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),
                   New_Index = c(1, 2, 1, 2, 1))

> 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$New_Index)

> data_sparse

2 x 3 sparse Matrix of class "dgCMatrix"
     A B C
[1,] 0 3 7
[2,] 5 5 .
data <- 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))

> 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)

> data_sparse

2 x 3 sparse Matrix of class "dgCMatrix"
       A B C
276725 0 3 7
276726 5 5 .
further explanation with code.
Source Link
wacax
  • 3.5k
  • 4
  • 26
  • 48

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

data <- 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),
                   New_Index = c(1, 2, 1, 2, 1))

> 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$New_Index)

> data_sparse

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

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

data <- 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),
                   New_Index = c(1, 2, 1, 2, 1))

> 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$New_Index)

> data_sparse

2 x 3 sparse Matrix of class "dgCMatrix"
     A B C
[1,] 0 3 7
[2,] 5 5 .
clarification requested by comment.
Source Link
wacax
  • 3.5k
  • 4
  • 26
  • 48

Your data looks like this at the moment:

data <- 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))

> 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)

> 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.

Your data looks like this at the moment:

data <- 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))

> 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)

> 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.

Your data looks like this at the moment:

data <- 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))

> 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)

> 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.

Source Link
wacax
  • 3.5k
  • 4
  • 26
  • 48
Loading