2011-12-23 14 views
3

Rのwordcloudパッケージを使用してwordcloudを作成していて、 "Word Cloud in R"の助けを借りています。ワードクラウドから単語を削除するにはどうすればよいですか?

私はこれを簡単に行うことができますが、このワードクラウドから単語を削除したいと思います。私はファイル内に単語を持っています(実際にはExcelファイルですが、それを変更することができます)。これらの単語はすべて除外したいと思います。助言がありますか?

require(XML) 
require(tm) 
require(wordcloud) 
require(RColorBrewer) 
ap.corpus=Corpus(DataframeSource(data.frame(as.character(data.merged2[,6])))) 
ap.corpus=tm_map(ap.corpus, removePunctuation) 
ap.corpus=tm_map(ap.corpus, tolower) 
ap.corpus=tm_map(ap.corpus, function(x) removeWords(x, stopwords("english"))) 
ap.tdm=TermDocumentMatrix(ap.corpus) 
ap.m=as.matrix(ap.tdm) 
ap.v=sort(rowSums(ap.m),decreasing=TRUE) 
ap.d=data.frame(word = names(ap.v),freq=ap.v) 
table(ap.d$freq) 
+4

の代わりに、またはに加えて、 'ストップワード(「英語」)' Excelからストップワードは、同様にファイルを追加します。単語のベクトルを組み合わせて、1つのストップワードのベクトルを作ることができます。これらは雲から除外されています。 –

答えて

3

@Tyler Rinkerは、もう少し詳細をremoveWords()の別の行を追加しますが、ここだ、答えを与えています。

はのは、あなたのExcelファイルをnuts.xlsと呼ばれ、あなたがこの

 library(gdata) # package with xls import function 
    library(tm) 
    # now load the excel file with the custom stoplist, note a few of the arguments here 
    # to clean the data by removing spaces that excel seems to insert and prevent it from 
    # importing the characters as factors. You can use any args from read.table(), which is 
    # handy 
    nuts<-read.xls("nuts.xls", header=TRUE, stringsAsFactor=FALSE, strip.white=TRUE) 

    # now make some words to build a corpus to test for a two-step stopword removal process... 
    words1<- c("peanut, cashew, walnut, macadamia, apple, pear, orange, lime, mandarin, and, or, but") 
    words2<- c("peanut, cashew, walnut, almond, apple, pear, orange, lime, mandarin, if, then, on") 
    words3<- c("peanut, walnut, almond, macadamia, apple, pear, orange, lime, mandarin, it, as, an") 
    words.all<-data.frame(rbind(words1,words2,words3)) 
    words.corpus<-Corpus(DataframeSource((words.all))) 

    # now remove the standard list of stopwords, like you've already worked out 
    words.corpus.nostopwords <- tm_map(words.corpus, removeWords, stopwords("english")) 
    # now remove the second set of stopwords, this time your custom set from the excel file, 
    # note that it has to be a reference to a character vector containing the custom stopwords 
    words.corpus.nostopwords <- tm_map(words.corpus.nostopwords, removeWords, nuts$stopwords) 

    # have a look to see if it worked 
    inspect(words.corpus.nostopwords) 
    A corpus with 3 text documents 

    The metadata consists of 2 tag-value pairs and a data frame 
    Available tags are: 
      create_date creator 
    Available variables in the data frame are: 
      MetaID 

    $words1 
     , , , , apple, pear, orange, lime, mandarin, , , 

    $words2 
     , , , , apple, pear, orange, lime, mandarin, , , 

    $words3 
     , , , , apple, pear, orange, lime, mandarin, , , 

成功のように進むかもしれませんRでこの

stopwords 
peanut 
cashew 
walnut 
almond 
macadamia 

のような言葉の単一の列を持っているとしましょう! Excelのカスタムリストの単語と同様に、標準ストップワードはなくなりました。間違いなく、それを行うための他の方法があります。

+0

BenとTin Manの両方に感謝します。 2人の何らかの組み合わせが私のために働いた。私はgdataでxlsを読み込むのに問題がありました。なぜなら、それがマスクされていたとしたら、私の問題はexcelの余分なスペースと複数の単語のセルであることが判明したからです。私はそれをすべて感謝します!ありがとう! – user1108155

0

データをデータフレームに変換するデータをデータフレームに変換します。 削除したい単語を含むCSVファイルを作成し、それをデータフレームとして読み込みます。その後、anti_join作ることができます。

関連する問題