【问题标题】:Creating an R wordcloud for a dataframe with a "word" and "count" column (NOVICE)为具有 \"word\" 和 \"count\" 列的数据框创建 R wordcloud(新手)
【发布时间】:2022-11-10 22:12:48
【问题描述】:

我对 R 很陌生,并试图了解一些情节。

我试图从一个数据框中显示哪些单词最常出现,该数据框中有一个单词本身的列,然后是出现次数的“计数”列。是否可以使用这些信息创建一个词云图?从我只找到关于使用原始文本的指南,我无权访问。

【问题讨论】:

  • 如果您将数据样本发布为文本而不是图像。我必须通过 OCR 软件传递您上次发布的图像来提取数据。此外,对于词云生成,字母会看起来很糟糕。您能否用单词而不是字母组成一个示例数据框并发布实际文本?将您制作的数据框存储为df,然后调用dput(df)-该函数的输出将为您提供一个字符串,您可以将其复制到您的问题中,以便这里的人们可以重现它。

标签: r word-cloud


【解决方案1】:

用这种格式的数据很容易得到一个词云。我不得不根据您在图像中显示的格式创建一个虚拟数据框(见下文),但代码只是:

library(wordcloud2)

wordcloud2(spamwordcountdf)


使用的数据

spamwordcountdf <- structure(list(word = c(
  "Unsecured debt", "Enlargement", "Money", 
"Valium", "Viagra", "Vicodin", "Weight loss", "Opportunity", 
"Xanax", "Bank", "Warning", "Suspicious", "Act now", "Don't delay", 
"Wonderful"), count = c(151062L, 109878L, 105425L, 103997L, 100761L, 
90840L, 89645L, 83720L, 58852L, 54181L, 47605L, 44695L, 42336L, 
38793L, 36867L)), row.names = c("41", "55", "7", "139", "15", 
"276", "174", "24", "73", "141", "46", "228", "242", "78", "130"
), class = "data.frame")

spamwordcountdf
#>               word  count
#> 41  Unsecured debt 151062
#> 55     Enlargement 109878
#> 7            Money 105425
#> 139         Valium 103997
#> 15          Viagra 100761
#> 276        Vicodin  90840
#> 174    Weight loss  89645
#> 24     Opportunity  83720
#> 73           Xanax  58852
#> 141           Bank  54181
#> 46         Warning  47605
#> 228     Suspicious  44695
#> 242        Act now  42336
#> 78     Don't delay  38793
#> 130      Wonderful  36867

reprex package (v2.0.1) 于 2022 年 5 月 22 日创建

【讨论】:

    猜你喜欢
    • 2016-11-25
    • 2022-01-03
    • 2015-04-21
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多