[R] write.matrix.csr(e1071) bug
Jeszenszky Peter
jeszy at inf.unideb.hu
Thu Oct 23 18:36:00 CEST 2008
Hello,
The write.matrix.csr() function of the e1071 package contains a bug.
Try the following:
library(e1071)
m <- 1 - diag(10)
sm <- as.matrix.csr(m)
write.matrix.csr(sm)
The resulting file (out.dat) contains only the two lines below:
2:1 3:1 4:1 5:1 6:1 7:1 8:1 9:1 10:1
1:1 3:1 4:1 5:1 6:1 7:1 8:1 9:1 10:1
This is obviously wrong as the matrix m has 90 non-zero entries.
The function is implemented as
write.matrix.csr <- function (x, file="out.dat", y=NULL) {
on.exit(sink())
if(!require("methods")) stop("Could not load package 'methods'.")
if (!is.null(y) & (length(y) != nrow(x)))
stop(paste("Length of y (=", length(y),
") does not match number of rows of x (=",
nrow(x), ")!", sep=""))
sink(file)
for (i in 1:nrow(x)) {
if (!is.null(y)) cat (y[i],"")
if (x at ia[i] <= nrow(x))
for (j in x at ia[i]:(x at ia[i+1] - 1))
cat(x at ja[j], ":", x at ra[j], " ", sep="")
cat("\n")
}
}
The bug is caused by the line
if (x at ia[i] <= nrow(x))
that is completely meaningless. In the above example nrow(x) is 10
and x at ia is the vector
1 10 19 28 37 46 55 64 73 82 91
Entries of x at ia has nothing to do with the number of rows in the matrix.
To fix the bug this line must be removed.
Best regards,
Peter
More information about the R-help
mailing list