[R] package:Matrix handling of data with identical indices

Thaden, John J ThadenJohnJ at uams.edu
Sun Jul 9 05:45:57 CEST 2006


In the Matrix package v. 0.995-11 I see that the dgTMatrix
Class for compressed, sparse, triplet-form matrices handles
Identically indexed data instances by summing their values,
e.g., 

library(Matrix)
(Mt <- new("dgTMatrix", 
   i = as.integer(c(0,0,1,1,4)),
   j = as.integer(c(0,1,2,2,4)),
   x = as.double(1:5),
   Dim = as.integer(c(5,5))))
## 5 x 5 sparse Matrix of class "dgTMatrix"
## [1,] 1 2 . . .
## [2,] . . 7 . .    <--- 7 = 3 + 4.
## [3,] . . . . .
## [4,] . . . . .
## [5,] . . . . 5

# If instead I make a dgCMatrix-class matrix, the first
# instance is overwritten by the second, e.g.,

library(Matrix)
(Mc <- new("dgCMatrix", 
   i = as.integer(c(0,0,1,1,4)),
   p = as.integer(c(0,1,2,4,5)),
   x = as.double(1:5),
   Dim = as.integer(c(5,5))))
## 5 x 5 sparse Matrix of class "dgCMatrix"
##             
## [1,] 1 2 . .
## [2,] . . 4 .   <-- the datum '3' has been lost.
## [3,] . . . .
## [4,] . . . .
## [5,] . . . 5 

# If one arrives at the dgCMatrix via the dgTMatrix class,
# the summed value is of course preserved, e.g.,

(Mtc <- as(Mt, "dgCMatrix"))
## 5 x 5 sparse Matrix of class "dgCMatrix"
##               
## [1,] 1 2 . . .
## [2,] . . 7 . .
## [3,] . . . . .
## [4,] . . . . .
## [5,] . . . . 5

As there is nothing inherent in either compressed, sparse,
format that would prevent recognition and handling of
duplicated index pairs, I'm curious why the dgCMatrix
class doesn't also add x values in those instances?
I wonder also if others might benefit also by being able
to choose how these instances are handled, i.e.,
whether they are summed, averaged or overwritten?  

-John Thaden, Ph.D.
Research Assistant Professor of Geriatrics
University of Arkansas for Medical Sciences
Little Rock AR, USA


Confidentiality Notice: This e-mail message, including any a...{{dropped}}



More information about the R-help mailing list