我目前正在学习有关R和Im的方法,但受到以下问题的困扰:
我有一个像这样建立的数据框
word freq1 freq2
tree 10 20
this 2 3
that 4 5
...
它显示在文本1(freq1)和文本2(freq2)中使用单词的频率。是否可以将其转换为term-document-matrix?我需要它成为term-document-matrix才能应用以下功能
par(mfrow=c(1,1))
comparison.cloud(tdm, random.order=FALSE, colors =
c("indianred3","lightsteelblue3"),
title.size=2.5, max.words=400)
来自https://rpubs.com/brandonkopp/creating-word-clouds-in-r
谢谢:)
答案 0 :(得分:1)
编辑:重塑数据后:
library(reshape2)
library(tm)
library(dplyr)
library(wordcloud)
df2<-df %>%
gather("Origin","Freq",c(2,3)) %>%
acast(word~Origin,fill=0,value.var = "Freq")
comparison.cloud(df2, random.order=FALSE, colors = c("indianred3","lightsteelblue3"),
max.words=400)
原始答案: 您的数据存在问题。这是导致字云或比较云的基本工作流程。
library(tm)
library(dplyr)
library(wordcloud)
df<-read.table(text="word freq1 freq2
Tree 10 20
This 2 3
That 4 5",header=T)
df$word<-as.character(df$word)
df1<-df %>%
gather()
corpus_my<-Corpus(VectorSource(df1))
tdm<-as.matrix(TermDocumentMatrix(corpus_my))
comparison.cloud(tdm, random.order=FALSE, colors = c("indianred3","lightsteelblue3"),
max.words=400)