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?


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

[1] 4449    6

[1] 4449  904

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)

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.