R函数将文本文件转换为文档术语矩阵

R function converting text file into document term matrix

我有一个包含三列的文本文件,它们是文档 ID、术语 ID 和术语频率。是否有将此数据转换为文档术语矩阵的 R 函数?

例如

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