## Coercion of matrix to sparse matrix (dgCMatrix) and maintaining dimnames.

Søren Højsgaard — written Jan 20, 2013 — source

Consider the following matrix

```[1] 0.1667
```
```  a b c d e f
a 0 0 0 0 0 1
b 0 0 0 1 0 1
c 0 0 0 0 0 0
d 0 0 0 0 0 0
e 1 1 0 0 0 0
f 0 0 0 1 0 0
```

This matrix can be coerced to a sparse matrix with

```Loading required package: methods
```
```6 x 6 sparse Matrix of class "dgCMatrix"
a b c d e f
a . . . . . 1
b . . . 1 . 1
c . . . . . .
d . . . . . .
e 1 1 . . . .
f . . . 1 . .
```
```Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
..@ i       : int [1:6] 4 4 1 5 0 1
..@ p       : int [1:7] 0 1 2 2 4 4 6
..@ Dim     : int [1:2] 6 6
..@ Dimnames:List of 2
.. ..\$ : chr [1:6] "a" "b" "c" "d" ...
.. ..\$ : chr [1:6] "a" "b" "c" "d" ...
..@ x       : num [1:6] 1 1 1 1 1 1
..@ factors : list()
```

Using Eigen via RcppEigen we can obtain the coercion as:

```6 x 6 sparse Matrix of class "dgCMatrix"
a b c d e f
a . . . . . 1
b . . . 1 . 1
c . . . . . .
d . . . . . .
e 1 1 . . . .
f . . . 1 . .
```
```Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
..@ i       : int [1:6] 4 4 1 5 0 1
..@ p       : int [1:7] 0 1 2 2 4 4 6
..@ Dim     : int [1:2] 6 6
..@ Dimnames:List of 2
.. ..\$ : chr [1:6] "a" "b" "c" "d" ...
.. ..\$ : chr [1:6] "a" "b" "c" "d" ...
..@ x       : num [1:6] 1 1 1 1 1 1
..@ factors : list()
```
```[1] TRUE
```

Compare the performance:

```                 test replications elapsed relative user.self sys.self
1 asdgCMatrix_(m * 1)         1000   0.028     1.00     0.028    0.000
2  as(m, "dgCMatrix")         1000   0.287    10.25     0.284    0.004
```

For larger matrices the difference in performance gain is smaller:

```                 test replications elapsed relative user.self sys.self
1 asdgCMatrix_(m * 1)         1000   0.133    1.000     0.132    0.000
2  as(m, "dgCMatrix")         1000   0.359    2.699     0.356    0.004
```
```                 test replications elapsed relative user.self sys.self
1 asdgCMatrix_(m * 1)          100   1.193     1.00     1.184    0.004
2  as(m, "dgCMatrix")          100   2.303     1.93     2.092    0.204
```
```                 test replications elapsed relative user.self sys.self
1 asdgCMatrix_(m * 1)          100   8.868    1.000      5.82    3.004
2  as(m, "dgCMatrix")          100  23.441    2.643     18.70    4.636
```

Thanks to Doug Bates for illustrating to me how set the dimnames attribute.

tags: eigen  matrix  sparse