0

I have a dtm and want to extract the top 5 terms by frequency for each document from the document term matrix.

I have a dtm built using the tm package

  Terms                     
Docs aaaa aac abrt abused accept accepted
1 0 0 0 0 0 0 
2 0 0 0 0 0 0
3 0 0 0 0 0 0
4 0 0 0 0 0 0
5 0 0 0 0 0 0
6 0 0 0 0 0 0

required output should be of the form:

Id   
1   Term1 Term2 Term3 Term4 Term5
2   Term1 Term2 Term3 Term4 Term5
and so on for all the documents.

I have tried all the solutions available from stackoverflow ans other sources like Make dataframe of top N frequent terms for multiple corpora using tm package in R (converted to tdm and tried to bring to the output form but did not work)and others but noting seem to work.

aydinugur
  • 1,208
  • 2
  • 14
  • 21
Bhavya
  • 3
  • 2

1 Answers1

1

Using Quanteda:

library(quanteda)
txt <- c("hello world world fizz", "foo bar bar buzz")
dfm <- dfm(txt)
topfeatures(dfm, n = 2, groups = seq_len(ndoc(dfm)))
# $`1`
# world hello 
# 2     1 
# 
# $`2`
# bar foo 
# 2   1 

You can also convert between DocumentTermMatrix and dfm.

Or using the classical tm:

library(tm)
packageVersion("tm")
# [1] ‘0.7.1’
txt <- c(doc1="hello world world", doc2="foo bar bar fizz buzz")
dtm <- DocumentTermMatrix(Corpus(VectorSource(txt)))
n <- 5
(top <- findMostFreqTerms(dtm, n = n))
# $doc1
# world hello 
# 2     1 
# 
# $doc2
# bar buzz fizz  foo 
# 2    1    1    1 
do.call(rbind, lapply(top, function(x) { x <- names(x);length(x)<-n;x }))
# [,1]    [,2]    [,3]   [,4]  [,5]
# doc1 "world" "hello" NA     NA    NA  
# doc2 "bar"   "buzz"  "fizz" "foo" NA 

findMostFreqTerms is available since tm version 0.7-1.

lukeA
  • 53,097
  • 5
  • 97
  • 100