m3m {chinese.misc} | R Documentation |
Convert Objects among matrix, dgCMatrix, simple_triplet_matrix, DocumentTermMatrix, TermDocumentMatrix
Description
This is to convert objects conveniently. The three types of matrix are 1st, "matrix"; 2nd, "dgCMatrix" in package Matrix; 3rd, "simple_triplet_matrix", "DocumentTermMatrix", "TermDocumentMatrix" in package slam, tm. This function is to be used when you read a csv file and want it to be a dtm; or, when you have a very large dtm and you want it to be saved or passed to another function that deals with dgCMatrix object. Note, it cannot convert between simple_triplet_matrix on one side, and dtm or tdm on the other.
Usage
m3m(x, to, keep_name = TRUE)
Arguments
x |
object of class matrix, dgCMatrix, simple_triplet_matrix, DocumentTermMatrix, TermDocumentMatrix. |
to |
to what class do you want to convert |
keep_name |
whether to keep names or
dimnames, which are, for dtm-like object,
documents and terms. |
Value
the object whose class is specified by argument to
.
Examples
# Make a matrix and convert to a dtm
m <- sample(0: 1, 50, replace = TRUE)
m <- matrix(m, nrow = 5)
colnames(m) <- letters[1: 10]
rownames(m) <- as.character(1: 5)
dtm <- m3m(m, "dtm")
# Convert dtm to dgCMatrix
M <- m3m(dtm, "M")