You get an error message, because, as you commented, you reach the limit of the integer limit, normal, because you have a huge number of documents .. This reproduces the error:
as.integer(.Machine$integer.max+1) [1] NA Warning message: NAs introduced by coercion
A vector function that takes an integer as a parameter fails because the second parameter is NA.
One solution is to override as.matrix.simple_triplet_matrix without calling vector . For instance:
as.matrix.simple_triplet_matrix <- function (x, ...) { nr <- x$nrow nc <- x$ncol
But I'm not sure that it is a good idea to force such a sparse matrix into the matrix (100%).
EDIT
One idea is to use the saparseMatrix package from Matrix . Here is an example where I compare the objects generated by each coercion. You get 10 percent income (I think that with respect to your very sparse matrix, you will get more) using sparseMatrix . Moreover, addition and multiplication are supported by a sparse matrix.
require(tm) data("crude") dtm <- TermDocumentMatrix(crude, control = list(weighting = weightTfIdf, stopwords = TRUE)) library(Matrix) Dense <- sparseMatrix(dtm$i,dtm$j,x=dtm$v) dense <- as.matrix(dtm)
source share