Search code examples
rsparse-matrix

Loading a sparse matrix in r from list


Hi I have a file having a structure as follows

12    45    56
34    65    31
12    23    43

and so on I have a huge dataset

so I have text file having 3 columns but, the way I want to create the sparse matrix is that for each line say 12 45 56..... first number i.e is the row second number i.e 45 is column and third number(i.e 56) is the value at 12th row and 45th column of the sparse matrix

I do the following

>x = scan('data.txt',what=list(integer(),integer(),numeric()))
Read 61944406 records

> library('Matrix')
Loading required package: lattice
N
> N= sparseMatrix(i=x[[1]],j=x[[2]],x=x[[3]])

but I get this error

Error in validObject(r) : 
invalid class “dgTMatrix” object: all row indices (slot 'i') must be between 0 and nrow-1 in a TsparseMatrix

could anyone help me figure out what I am doing wrong?


Solution

  • When you use x[[1]] you are refering to the first element of you x matrix, in this case x[[1]]=12. You must use x[,1] if you want index columms or x[1,] for rows. Try this:

    x = matrix(c(12,45,56,
                 34,65,31,
                 12,23,43), nrow=3, byrow=TRUE)
    
    N= sparseMatrix(i=x[,1], j=x[,2], x=x[,3])
    

    EDITED: I have reproduced your error:

    x = matrix(c( 0,45,56,
                 34,65,31,
                 12,23,43), nrow=3, byrow=TRUE)
    
    N= sparseMatrix(i=x[,1], j=x[,2], x=x[,3])
    Error en validObject(r) : 
      invalid class “dgTMatrix” object: all row indices (slot 'i') must be between 0 and nrow-1 in a TsparseMatrix
    

    So, make sure you don't have 0 in your 2 first colums