I have text file with three columns which are document id, term id and term frequency. Is there an R function that converts this data to document term matrix?
Asked
Active
Viewed 575 times
0
-
Please read [how to provide a minimal reproducible example in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610) - questions should provide input data (e.g. add the result of `dput(mydata)`), the expected output, what lines of code you tried and in what way they failed. – lukeA Jun 06 '16 at 16:01
1 Answers
2
For example
df <- read.table(header=T, text='"doc" "term" "freq"
1 "foo" 1
1 "bar" 2
2 "hello" 1
2 "world" 2')
library(tm)
dtm <- as.DocumentTermMatrix(xtabs(freq~doc+term, df), weighting=weightTf)
as.matrix(dtm)
# Terms
# Docs bar foo hello world
# 1 2 1 0 0
# 2 0 0 1 2

lukeA
- 53,097
- 5
- 97
- 100