[R] Creating a sparse matrix from a file

Pallavi Palleti pallavip.05 at gmail.com
Tue Oct 27 05:19:29 CET 2009


Hi David,

Thanks for your help. This is exactly what I want.
But, I have number of rows of my matrix = 25k and columns size as 80k. So,
when I define a matrix object, it is throwing an error saying can not
allocate a vector of length (25K * 80k). I heard that, this data can still
be loaded into R using sparseMatrix. However, I couldn't get a syntax for
creating the same.  Could someone kindly help me in this regard.

Thanks
Pallavi


David Winsemius wrote:
> 
> 
> On Oct 26, 2009, at 5:06 AM, Pallavi Palleti wrote:
> 
>>
>> Hi all,
>>
>> I am new to R and learning the same. I would like to create a sparse  
>> matrix
>> from an existing file whose contents are in the format
>>      "rowIndex,columnIndex,value"
>>
>> for ex:
>> 1,2,14
>> 2,4,15
>>
>> I would like to create a sparse matrix by taking the above as input.
>> However, I couldn't find an example where the data was being read  
>> from a
>> file. I tried searching in R tutorial and also searched for the same  
>> in web
>> but in vain. Could some one kindly help me how to give the above  
>> format as
>> input in R to create a sparse matrix.
> 
>   ex <- read.table(textConnection("1,2,14
>   2,4,15") , sep=",")
>   ex
> #  V1 V2 V3
> #1  1  2 14
> #2  2  4 15
> 
>   M <- Matrix(0, 20, 20)
> 
>  > M
> #20 x 20 sparse Matrix of class "dsCMatrix"
> 
>   [1,] . . . . . . . . . . . . . . . . . . . .
>   [2,] . . . . . . . . . . . . . . . . . . . .
>   [3,] . . . . . . . . . . . . . . . . . . . .
> snip
> 
>   for (i in 1:nrow(ex) ) { M[ex[i, 1], ex[i, 2] ] <- ex[i, 3] }
> 
>  > M
> 20 x 20 sparse Matrix of class "dgCMatrix"
> 
>   [1,] . 14 .  . . . . . . . . . . . . . . . . .
>   [2,] .  . . 15 . . . . . . . . . . . . . . . .
>   [3,] .  . .  . . . . . . . . . . . . . . . . .
> snip
>  >
> --
> 
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 

-- 
View this message in context: http://www.nabble.com/Creating-a-sparse-matrix-from-a-file-tp26056334p26071964.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list