Sparse matrix to a data frame in R

Viewed 22240

I have a sparse matrix

Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:37674] 1836 2297 108 472 1735 1899 2129 2131 5 67 ...
  ..@ p       : int [1:3417] 0 2 8 22 25 35 44 45 45 47 ...
  ..@ Dim     : int [1:2] 3416 3416
  ..@ Dimnames:List of 2
  .. ..$ : chr [1:3416] "AAA" "AAE" "AAL" "AAN" ...
  .. ..$ : chr [1:3416] "AAA" "AAE" "AAL" "AAN" ...
  ..@ x       : num [1:37674] 1 1 1 1 1 1 1 1 1 1 ...
  ..@ factors : list()

What is a fast way to convert this matrix to a list as (except for a for loop):

Origin Destination Weight
AAA AAE 4
AAL AAN 5

Note: I only need to get the Origin and Destination for Weight>0

3 Answers
df <- as.data.frame(as.matrix(m))

If the sparse matrix is to large, I found that converting it in chunks works. Modify the "step_size" variable below to change the chunk size. Change "ncol" and "cbind" to "nrow" and "rbind" if you want to chunk it row-wise instead of column-wise.

large_matrix_to_df <- function(m){
  
  iter = 1
  step_size = 5000
  
  output = NULL
  
  while(iter + step_size < ncol(m)){
    print(iter)
    add_output = data.frame(as.matrix(m[,iter:(iter+step_size-1)]))

    if(is.null(output)){
      output = add_output
    }else{
      output = cbind(output,add_output)
    }
    
    iter = iter + step_size
  }

  add_output = data.frame(as.matrix(m[,iter:ncol(m)]))
  output = cbind(output,add_output)
  
  return (output)
}
Related