2016-09-01 23 views
0

ニュースタイトルのようなpdfファイル(新聞の段落から抜粋)に内容があります - 「これは成長の最速で、セクタ。"。今私はいくつかの手続きか、文字列内の単語の数を数えたいものがあります。 単語r言語で文字列内の各単語の出現数を調べる方法

[1]この
は、[1]
ある---------単語がを発生し、時間の

数:私はそれを実行するとその結果は次のようにする必要があります [2]

[2]の[2]

など。

あなたのご協力をお待ちしております。この例では

+0

'stringi :: stri_count_words'? – hrbrmstr

答えて

3

library(stringr) 
library(data.table) 

s <- " this is one of the fastest and one of the growing sector." 

ss <- data.frame(x=unlist(str_split(s, " "))) 

sss <- setDT(ss)[, .(freq = .N), x] 

sss: 

     x freq 
1:   1 
2: this 1 
3:  is 1 
4:  one 2 
5:  of 2 
6:  the 2 
7: fastest 1 
8:  and 1 
9: growing 1 
10: sector. 1 

あなたはまた、事前に句読点を削除するstr_splitを使用することができます:

gsub("[[:punct:]]", "", s) 
関連する問題