Skip to content

Request as.dfm.DocumentTermMatrix & as.dfm.TermDocumentMatrix #1222

Description

@trinker

It is nice to be able to convert between data structures that are common in the text world.
As the dfm becomes one of the preferred structures it'd be nice to convert from another structure that is common the DocumentTermMatrix & TermDocumentMatrix . Granted, I'm sure you'd prefer people just go from Corpus to dfm in quanteda but sometimes you have one of thes tm structures already because of preference or some pakage output or just to give the user flexibility.

quanteda already provides the as.dfm conversion for matrices and data.frames. I'd like to request extensions of as.dfm.DocumentTermMatrix & as.dfm.TermDocumentMatrix that allow for easy conversion from these data forms. Below I provide the current code I use for such conversion though I realize you're using more sophisticated methods with as_dfm_constructor under the hood. I also get that this flexibility may be in antithesis of the quanteda in which case I'll accept "This is out of scope" as a response. But I figure I might ask and see.

Also, I may have overlooked the fact that quanteda already does this. If this is the case kindly tell me I'm a bone head and the next time we meet up your first beer is on me :-)

Crude code for converting to dfm

as.dfm.DocumentTermMatrix <- function(x, ...){

    as.dfm(
        Matrix::sparseMatrix(
            i = x$i, 
            j = dtm$j, 
            x = x$v, 
            dimnames = list(seq_len(nrow(x)), colnames(x))
        )
    )

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions