m3m: Convert Objects among matrix, dgCMatrix,...

Description Usage Arguments Value Examples

View source: R/m3m.R

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

1
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 x to. Abbreviations can be used: "matrix" and "m" mean "matrix": "dgCMatrix" and "M" mean "dgCMatrix"; "simple_triplet_matrix" and "stm" mean "simple_triplet_matrix"; "DocumentTermMatrix", "dtm", "DTM" mean "DocumentTermMatrix"; "TermDocumentMatrix", "tdm", "TDM" mean "TermDocumentMatrix".

keep_name

whether to keep names or dimnames, which are, for dtm-like object, documents and terms. TRUE by default. If you set it to FALSE, you will lose them. But if you convert dgCMatrix to dtm or tdm, it is required that the dgCMatrix object has a list of length 2 as dimnames.

Value

the object whose class is specified by argument to.

Examples

1
2
3
4
5
6
7
8
# 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")

Example output

Warning messages:
1: In Sys.setlocale(category = "LC_COLLATE", s_right_locale) :
  OS reports request to set locale to "zh_CN.UTF-8" cannot be honored
2: In Sys.setlocale(category = "LC_CTYPE", s_right_locale) :
  OS reports request to set locale to "zh_CN.UTF-8" cannot be honored
Warning messages:
1: In Sys.setlocale(category = "LC_COLLATE", s_right_locale) :
  OS reports request to set locale to "zh_CN.UTF-8" cannot be honored
2: In Sys.setlocale(category = "LC_CTYPE", s_right_locale) :
  OS reports request to set locale to "zh_CN.UTF-8" cannot be honored

chinese.misc documentation built on Sept. 13, 2020, 5:13 p.m.