quanteda (version 4.0.1)

convert: Convert quanteda objects to non-quanteda formats


Convert a quanteda dfm or corpus object to a format useable by other packages. The general function convert provides easy conversion from a dfm to the document-term representations used in all other text analysis packages for which conversions are defined. For corpus objects, convert provides an easy way to make a corpus and its document variables into a data.frame.


convert(x, to, ...)

# S3 method for dfm convert( x, to = c("lda", "tm", "stm", "austin", "topicmodels", "lsa", "matrix", "data.frame", "tripletlist"), docvars = NULL, omit_empty = TRUE, docid_field = "doc_id", ... )

# S3 method for corpus convert(x, to = c("data.frame", "json"), pretty = FALSE, ...)


A converted object determined by the value of to (see above). See conversion target package documentation for more detailed descriptions of the return formats.



a dfm or corpus to be converted


target conversion format, one of:


a list with components "documents" and "vocab" as needed by the function lda.collapsed.gibbs.sampler from the lda package


a DocumentTermMatrix from the tm package. Note: The tm package version of as.TermDocumentMatrix() allows a weighting argument, which supplies a weighting function for TermDocumentMatrix(). Here the default is for term frequency weighting. If you want a different weighting, apply the weights after converting using one of the tm functions. For other available weighting functions from the tm package, see TermDocumentMatrix.


the format for the stm package


the wfm format from the austin package


the "dtm" format as used by the topicmodels package


the "textmatrix" format as used by the lsa package


a data.frame of without row.names, in which documents are rows, and each feature is a variable (for a dfm), or each text and its document variables form a row (for a corpus)


(corpus only) convert a corpus and its document variables into JSON format, using the format described in jsonlite::toJSON()


a named "triplet" format list consisting of document, feature, and frequency


unused directly


optional data.frame of document variables used as the meta information in conversion to the stm package format. This aids in selecting the document variables only corresponding to the documents with non-zero counts. Only affects the "stm" format.


logical; if TRUE, omit empty documents and features from the converted dfm. This is required for some formats (such as STM) that do not accept empty documents. Only used when to = "lda" or to = "topicmodels". For to = "stm" format, omit_empty is always TRUE.


character; the name of the column containing document names used when to = "data.frame". Unused for other conversions.


adds indentation whitespace to JSON output. Can be TRUE/FALSE or a number specifying the number of spaces to indent. See prettify()


Run this code
## convert a dfm

toks <- corpus_subset(data_corpus_inaugural, Year > 1970) |>
dfmat1 <- dfm(toks)

# austin's wfm format
identical(dim(dfmat1), dim(convert(dfmat1, to = "austin")))

# stm package format
stmmat <- convert(dfmat1, to = "stm")

# triplet
tripletmat <- convert(dfmat1, to = "tripletlist")

if (FALSE) {
# tm's DocumentTermMatrix format
tmdfm <- convert(dfmat1, to = "tm")

# topicmodels package format
str(convert(dfmat1, to = "topicmodels"))

# lda package format
str(convert(dfmat1, to = "lda"))

## convert a corpus into a data.frame

corp <- corpus(c(d1 = "Text one.", d2 = "Text two."),
               docvars = data.frame(dvar1 = 1:2, dvar2 = c("one", "two"),
                                    stringsAsFactors = FALSE))
convert(corp, to = "data.frame")
convert(corp, to = "json")

Run the code above in your browser using DataLab