2012-11-30 7 views
47

tm_mapを使ってみました。それは次のエラーを出しました。どうすればこの問題を回避できますか?tm_map(...、tolower)でテキストを小文字に変換する際のエラー

require(tm) 
byword<-tm_map(byword, tolower) 

Error in UseMethod("tm_map", x) : 
    no applicable method for 'tm_map' applied to an object of class "character" 
+2

は何のパッケージです'tm_map'から?これは、いくつかの非ベースパッケージに依存しているようです。完全性のために 'library'ステートメントを含めることを検討してください。 –

+1

@DanielKrizian: 'tm_map()'は 'tm'パッケージからのもので、' tolower() 'は' base'からのものです – smci

答えて

101

ベースR機能tolower()使用します。この方法でTOLOWERを使用して

tolower(c("THE quick BROWN fox")) 
# [1] "the quick brown fox" 
+0

ありがとう。しかし、なぜ私はそのエラーを持っているかについての任意の洞察?他のtm_mapアプリケーションを使う必要があるかもしれません! – jackStinger

+0

'tm_map'のヘルプファイル(' tm'パッケージ内)は利用可能な変換関数のリストを示し、 'tolower'はそれらのうちの一つではありません。変換は、クラス 'コーパス'のオブジェクトで動作するS3メソッドであるように見えます。だから、 'tm_map'を使って関数を使うことはできません。 – bdemarest

3
myCorpus <- Corpus(VectorSource(byword)) 
myCorpus <- tm_map(myCorpus , tolower) 

print(myCorpus[[1]]) 
+9

'tm_map(myCorpus、content_transformer(tolower))' – daroczig

+0

@daroczigのように、 'VCorpus'オブジェクトを壊さないように' content_transformer'の中に 'tolower'をラップする必要があります。 – smci

+0

@smciのアイデアをありがとう、私はちょうど新しい答えとして上記のコメントを提出した:) – daroczig

1

を望ましくない副作用を持っている:あなたは後でコーパスのうち、長期文書行列を作成しようとした場合、それは失敗するでしょう。これは、tolowerの戻り値の型を処理できないtmの最近の変更が原因です。代わりに、使用:

myCorpus <- tm_map(myCorpus, PlainTextDocument) 
6

は、ここではより詳細な回答に私のcommentを展開する:あなたはVCorpusオブジェクトを台無しにしないcontent_transformerの内側tolowerをラップする必要がある - のようなもの:

> library(tm) 
> data('crude') 
> crude[[1]]$content 
[1] "Diamond Shamrock Corp said that\neffective today it had cut its contract prices for crude oil by\n1.50 dlrs a barrel.\n The reduction brings its posted price for West Texas\nIntermediate to 16.00 dlrs a barrel, the copany said.\n \"The price reduction today was made in the light of falling\noil product prices and a weak crude oil market,\" a company\nspokeswoman said.\n Diamond is the latest in a line of U.S. oil companies that\nhave cut its contract, or posted, prices over the last two days\nciting weak oil markets.\n Reuter" 
> tm_map(crude, content_transformer(tolower))[[1]]$content 
[1] "diamond shamrock corp said that\neffective today it had cut its contract prices for crude oil by\n1.50 dlrs a barrel.\n the reduction brings its posted price for west texas\nintermediate to 16.00 dlrs a barrel, the copany said.\n \"the price reduction today was made in the light of falling\noil product prices and a weak crude oil market,\" a company\nspokeswoman said.\n diamond is the latest in a line of u.s. oil companies that\nhave cut its contract, or posted, prices over the last two days\nciting weak oil markets.\n reuter" 
関連する問題