In the R language, I have an S4 DataFrame consisting of Rle encoded elements.

The data can be simulated using following code

x = DataFrame(Rle(1:10),Rle(11:20),Rle(21:30))

Now, I want to convert this DataFrame to a sparse matrix from the Matrix package. On a usual data.frame, one can do

` Matrix(x,sparse=TRUE)`

However, this does not work for DataFrames, as it gives the following error:

`Error in as.vector(data) : ` ` no method for coercing this S4 class to a vector`

Also, `Matrix(as.data.frame(x))`

does not work as it gives the following error:

`Error in asMethod(object) : invalid class 'NA' to dup_mMatrix_as_geMatrix `

Any ideas on how to convert between data types in a rather efficient way?

Thanks!

Hi Michael,

The proposed code indeed does work, however it is very inefficient, as you already mention yourself. I was hoping for a more efficient conversion between the two data types. The conversion you propose gives me memory issues for allocating the regular matrix.

I will work on this.

Is there an answer to this question?

Never got around to doing anything here. The S4Vectors package does not depend on Matrix, so I am not sure where this should go, but here is a simple way to go from Rle to Matrix (assuming the DataFrame is called "df"):

Thanks for quick response.

Just one little bug fix to make sure length is correct if Rle ends with zeros: