[R] SUMMARY: R crash with ''library(Matrix); as(x, "dgCMatrix")' [was: Warning while subsetting...]

Thaden, John J ThadenJohnJ at uams.edu
Sat Jul 8 20:52:56 CEST 2006

With thanks to Matrix package co-author Martin 
Maechler, I'm happy to report satisfactory closure 
of two recent threads I initiated about that package: 
   -  Warning while subsetting with Matrix
   -  R Crash with 'library(Matrix);as(x,"dgCMatrix")
In the first, I reported seeing a warning message after
selecting a subset of a matrix 'M' of class "dgTMatrix"
via the first but not the second of these commands:
  M10 <- M[1:10,]
  M10 <- M[1:10,1:10]

Acting on Martin's advice, I upgraded Matrix version 
0.995-10 to version 0.995-11.  This eliminated the
warning message.

In the second, I reported that class-conversion of 
a self-constructed "dgTMatrix" of dimension 600 X 4482
caused R to crash when done via either of the first two
but neither of the last two of these commands:

  Mc <- as(M, "dgCMatrix")
  Mm <- as(M, "matrix")
  Mc <- rbind(as(M[1:300,], "dgCMatrix"), as(M[301:600], "dgCMatrix")
  Mm <- rbind(as(M[1:300,], "matrix"), as(M[301:600], "matrix")

I surmised this was a memory issue but Martin pointed out 
the memory demand to hold even the expanded sparse matrix
is actually rather small (8 x 600 x 4482 = 21549456 bytes
= 21.5 Megabytes).
For obvious bandwidth reasons, I did not post my large 
matrix, so Martin created one of his own.  Neither he 
nor I could reproduce crashes with his matrix. Offlist,
I sent him my matrix. Upon class-conversion, it crashed 
his machine too.

Upon inspection, Martin discovered that my matrix did not
conform to the dgTMatrix class as defined in Matrix. In 
particular, my slots M at i and M at j were integer vectors 
beginning with ones, i.e., they respectively indexed the
first row and column of the matrix as "1", not as "0".  

However, correcting this did not stop crashes, until I also
realized that M at i had 601 (not 600) unique integer values, 
and corrected my Dims slot (M at Dims) to be c(601,4482).  This 
stopped the crashes.

Once created, Matrix objects behave like matrix objects in
the sense that "one-based" indexing is used to subset them
or re-assign values to them.  But for users who create 
them, especially using the new() function, it is helpful
to remind oneself that Matrix objects are created and 
stored using "zero-based" indexing.  This fact is
mentioned only obliquely in the current version's PDF file
and R-accessible documentation.  Hopefully that will change.

Again, my thanks to Martin. For me this experience confirms
The vigor of open-source, forum-supported projects.

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

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

More information about the R-help mailing list