【问题标题】:Using R to create a frequency word cloud using just a column使用 R 仅使用一列创建频率词云
【发布时间】:2014-12-24 02:49:05
【问题描述】:

如果我有一个简单的df如下:

mtcars 
               mpg cyl disp  hp drat   wt ... 
Mazda RX4     21.0   6  160 110 3.90 2.62 ... 
Mazda RX4 Wag 21.0   6  160 110 3.90 2.88 ... 
Datsun 710    22.8   4  108  93 3.85 2.32 ... 
               ............

假设第一列(row.names)是一长串汽车,我将如何仅使用该列创建频率词云。我尝试了wordcloud(mtcars$row.names),但出现以下错误:

UseMethod("TermDocumentMatrix", x) 中的错误:没有适用的方法 对于应用于“NULL”类对象的“TermDocumentMatrix”

也许 wordcloud 是错误的包?

【问题讨论】:

    标签: r frequency


    【解决方案1】:

    您是否尝试在控制台输入mtcars$row.names

    获取行名作为向量的方法是使用rownames(mtcars)。像这样:

    library(wordcloud) # this requires the tm and NLP packages
    
    wordcloud(rownames(mtcars), min.freq=1)  # w/o min.req=1, you get just "merc"
    

    【讨论】:

    • 非常感谢。这行得通。我对 [1:300000] 的字符向量尝试了相同的方法,并且 R 无限期地继续计算。我什至将频率提高到最大,但仍然没有。但是你解决了。谢谢!
    【解决方案2】:

    您可以按照以下步骤操作(我没有运行此代码):

    library(tm)
    
    # assuming your vector of names is mtcars$row.names, which I doubt but take for granted:
    
    text <- Corpus(VectorSource(mtcars$row.names))
    text <- tm_map(text, tolower) # might simplify the vector
    
    tdm <- TermDocumentMatrix(text) # turn it into a term document matrix
    
    library(wordcloud)
    m <- as.matrix(tdm)  
    freq <- sort(rowSums(m), decreasing = TRUE)
    wordcloud(words = names(freq), freq = freq, min.freq = 4, random.order = FALSE, 
              col=terrain.colors(8), scale=c(8,.2), max.words=100, rot.per=.15))
    

    【讨论】:

      猜你喜欢
      • 2013-03-08
      • 2016-05-12
      • 2018-09-09
      • 1970-01-01
      • 2015-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      相关资源
      最近更新 更多