1
$\begingroup$

I created a DocumentTermMatrix for text mining using RTextTools. The rows for this DocumentTermMatrix correspond to dataframe rows and matix columns correspond to words. My question is : How can I get the words (labels vector) for examining the DocumentTermMatrix ? In other words, How can I get the vector of these 904 words?

require(RTextTools,quietly=TRUE)
data(USCongress)

doc_matrix <- create_matrix(USCongress$text, language="english",   removeNumbers=TRUE, stemWords=TRUE, removeSparseTerms=.998)

dim(USCongress)
[1] 4449    6

dim(doc_matrix)
[1] 4449  904
$\endgroup$
2
$\begingroup$

a documenttermmatrix is a simple_triplet_matrix. You can turn this into a simple matrix with the as.matrix command and then use all matrix functions.

# turn into simple matrix
mat <- as.matrix(doc_matrix)

# vector of the words 
word_vector <- colnames(mat)

# Dataframe containing words and their frequency
df_words <- data.frame(words = colnames(mat), frequency = colSums(mat), row.names = NULL)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.