2016-04-24 9 views
-5
txt <- readLines("this.txt") 

library(tm) 

corpus <- Corpus(VectorSource(txt)) 

corpus <- tm_map (corpus, removePunctuation) 

tdm <- TermDocumentMatrix (corpus) 

m <- as.matrix (tdm) 

d <- data.frame(freq = sort(rowSums(m),decreasing = TRUE)) 

答えて

0

は、私はあなたがtmライブラリを使用して「」と「この」のような言葉を削除する方法を求めていると思いますか?

corpus <- tm_map(corpus, removeWords, c("hello","is","it","me","you're","looking","for?")) 

編集:特定の単語を削除するには

corpus <- tm_map(txt, removeWords, stopwords("english")) 

:もしそうなら、これを試してみてください、私は作品戦争と平和を、使用した例を作成しました。の前に用語を小文字に変換してください。文書用語行列を作成してください。これと同じように:

library(tm) 

# load 
txt <- readLines("this.txt") 
corpus <- Corpus(VectorSource(txt)) 

# clean 
corpus <- tm_map(corpus, removePunctuation) 
corpus <- tm_map(corpus, removeNumbers) 
corpus <- tm_map(corpus, tolower) 
corpus <- tm_map(corpus, removeWords, stopwords("english")) 
corpus <- tm_map(corpus, PlainTextDocument) 

# create dtm and get terms 
dtm <- DocumentTermMatrix(corpus) 
dtm$dimnames$Terms 

変更し、これにすべき類似したテキストファイルと出力に合わせてコード:

dtm$dimnames$Terms 
[1] "almost"   "anonymous"  "anyone"   "anywhere"  "author"   "away"   
[7] "aylmer"   "book"   "chapter"   "contents"  "copy"   "cost"   
[13] "date"   "david"   "ebook"   "english"   "give"   "gutenberg"  
[19] "iii"    "included"  "january"   "language"  "last"   "leo"    
[25] "license"   "louise"   "march"   "maude"   "may"    "one"    
[31] "online"   "peace"   "posting"   "project"   "restrictions" "reuse"   
[37] "start"   "terms"   "title"   "tolstoy"   "tolstoytolstoi" "translators"  
[43] "updated"   "use"    "vii"    "volunteer"  "war"    "whatsoever"  
[49] "widger"   "wwwgutenbergorg" 
+0

私はすでにこれをやろうとしていますが、うまくいきません...私のような言葉を削除する必要があります:dから、 –

+0

私はあなたが必要としていることを理解していますあなたのデータについてもっと具体的に: 残っている単語は何ですか? あなたのテキストはどの言語ですか? 残りの単語は大文字ですか、小文字ですか? 'The'ではなく' the'のような言葉があれば、それらを小文字に変換することができます。上記の私の編集を参照してください。 –

0

あなたのデータがどのように見えるかは分かりません。しかし、単純なfind replace関数であるgsubを使うことができます。

あなたは正規表現が何であるかを知っています
+0

すみませんが、Rは悲惨な私の人生を作る: '(:P –

+0

私が持っています行列Dと私は、などのいくつかの単語を削除する必要があります: コーパス

1

あなた

"Hello" 
を与える
gsub("The", "", "HelloThe") 

? R関数gsubについてhereを読むことができます。ここ は、それがどのように働くか少し例を示します

> let <- c("A", "B", "A", "C") # My vector of letters 
> let 
[1] "A" "B" "A" "C" 
> # I want delete "A", so this letter I will replace with nothing ("") 
> l <- gsub("A", "", let) # "A" replace by "" in vector let 
> l 
[1] "" "B" "" "C" 

あなたが今しなければならないのは、いずれかがある場合は、空の要素を削除します。

そして、あなたが唯一の1つのシンボルラインを持っている場合、その作品GSUB:

> let <- " a b c d g h a a a" 
> let 
[1] " a b c d g h a a a" 
> l <- gsub("a", "", let) 
> l 
[1] " b c d g h " 
+0

tnks ...しかしマトリックスを持っていればどうすればいいですか?私はそれをどうすればいいのですか? –

+0

マトリックス要素間でgsubが動作します。マトリックスをリストに変換する必要はありません。 KiprasやOliverによって書かれた例を使用することです。私はパッケージ 'tm'について少し知っているので、もっと理解するのを助けることはできません。 –

+0

ok thnk y sooooたくさん –

関連する問題