在R中创建文档频率矩阵
我试图在R中创建一个文档频率矩阵 我目前有一个数据帧(df_2),它由两列组成:在R中创建文档频率矩阵,r,machine-learning,R,Machine Learning,我试图在R中创建一个文档频率矩阵 我目前有一个数据帧(df_2),它由两列组成: doc_num:每个术语来自哪个文档的详细信息 text_token:包含与每个文档相关的每个标记化单词 df的尺寸为79447*2 但是,79447行中只有400个实际文档 我一直在尝试使用tm包创建此dfm 我尝试创建一个语料库(vectorsource),然后尝试使用 适当命名的“dfm”命令 但是,这表明“dfm()仅适用于字符、语料库、dfm和标记对象。” 据我所知,我的数据目前格式不正确,dfm命令无
是mise le meas。如果您的代码中提供了所有相关的详细信息,例如使用quanteda package for dfm(),这将对您和其他人非常有用。 如果基础文本设置正确,dfm()将直接为您提供所需内容—这正是它设置的目的。 下面是一个模拟:
library(tm)
library(quanteda)
# install.packages("readtext")
library(readtext)
doc1 <- "COVID-19 can be beaten if all ensure social distance, social distance is critical"
doc2 <- "COVID-19 can be defeated through early self isolation, self isolation is your responsibility"
doc3 <- "Corona Virus can be beaten through early detection & slowing of spread, Corona Virus can be beaten, Yes, Corona Virus can be beaten"
doc4 <- "Corona Virus can be defeated through maximization of social distance"
write.table(doc1,"doc1.txt",sep="\t",row.names=FALSE, col.names = F)
write.table(doc2,"doc2.txt",sep="\t",row.names=FALSE, col.names = F)
write.table(doc3,"doc3.txt",sep="\t",row.names=FALSE, col.names = F)
write.table(doc4,"doc4.txt",sep="\t",row.names=FALSE, col.names = F)
# save above into your WD
getwd()
txt <- readtext(paste0("Your WD/docs", "/*"))
txt
corp <- corpus(txt)
x <- dfm(corp)
View(x)
library(tm)
图书馆(quanteda)
#install.packages(“readtext”)
图书馆(readtext)
doc1您是否尝试过:使用(df_2,表(doc_num,text_token))
dplyr::count(df_2,doc_num,text_token)