2017-12-02 9 views
1

http://tidytextmining.com/sentiment.html#the-sentiments-datasetに基づいてセンチメント分析を実行しようとしています。感情分析を行う前に、自分のデータセットをきちんとしたフォーマットに変換する必要があります。ワードカウントでデータフレームをチブルに変換

私のデータセットの形式は次のとおりです。行につき1つの観察に変換するために

x <- c("test1" , "test2") 
y <- c("this is test text1" , "this is test text2") 
res <- data.frame("url" = x, "text" = y) 
res 
    url    text 
1 test1 this is test text1 
2 test2 this is test text2 

は、テキスト列を処理し、それがそのURLの出現単語と回数を含む新しい列を追加する必要があります。同じURLが複数の行に表示されます。ここで

は私の試みです:

library(tidyverse) 

x <- c("test1" , "test2") 
y <- c("this is test text1" , "this is test text2") 
res <- data.frame("url" = x, "text" = y) 
res 

res_1 <- data.frame(res$text) 
res_2 <- as_tibble(res_1) 
res_2 %>% count(res.text, sort = TRUE) 

返す:

# A tibble: 2 x 2 
      res.text  n 
       <fctr> <int> 
1 this is test text1  1 
2 this is test text2  1 

をRES $テキストデータフレーム内の単語をカウントし、感情分析を実行するためにURLを維持するためにどのように?

更新:

x <- c("test1" , "test2") 
y <- c("this is test text1" , "this is test text2") 
res <- data.frame("url" = x, "text" = y) 
res 

res %>% 
group_by(url) %>% 
transform(text = strsplit(text, " ", fixed = TRUE)) %>% 
unnest() %>% 
count(url, text) 

を返すエラー:あなたがhttp://tidytextmining.com/sentiment.html#the-sentiments-dataset

+0

なぜ変換する必要がありますか?言い換えれば、あなたのタイトルは実際の質問を表現していないようです。あなたは言葉がURLごとにできることを望んでいるようです。私は考えられる可能性のある1つのチベットのアプローチは、 'res%>%group_by(url)%>%transform(text = strsplit(text、" fixed、TRUE))%>%unnest()%>%count(url、text ) '(' text'は文字列で要素ではないと仮定します) –

+0

@DavidArenburg更新をご覧ください –

答えて

4

です:私は、これが評判分析をtidytextminingために必要なフォーマットのように見えるようtibbleに変換しようとしてる

Error in strsplit(text, " ", fixed = TRUE) : non-character argument 

このようなものを探していますか? tidytextパッケージで感情分析を処理する場合は、各文字列の単語をunnest_tokens()で区切る必要があります。この機能は、テキストを単語に分離する以上の機能を果たします。後でその機能を見たい場合。 1行につき1単語が得られたら、各テキストに各単語が何回表示されるのかを、count()を使って調べることができます。次に、ストップワードを削除します。 tidytextパッケージにはデータがあるので、それを呼び出すことができます。最後に、感情情報が必要です。ここではAFINNを選択しましたが、必要に応じて別のAFINNを選択することもできます。これがあなたに役立つことを願っています。

x <- c("text1" , "text2") 
y <- c("I am very happy and feeling great." , "I am very sad and feeling low") 
res <- data.frame("url" = x, "text" = y, stringsAsFactors = F) 

# url        text 
#1 text1 I am very happy and feeling great. 
#2 text2  I am very sad and feeling low 

library(tidytext) 
library(dplyr) 

data(stop_words) 
afinn <- get_sentiments("afinn") 

unnest_tokens(res, input = text, output = word) %>% 
count(url, word) %>% 
filter(!word %in% stop_words$word) %>% 
inner_join(afinn, by = "word") 

# url word  n score 
# <chr> <chr> <int> <int> 
#1 text1 feeling  1  1 
#2 text1 happy  1  3 
#3 text2 feeling  1  1 
#4 text2  sad  1 -2 
関連する問題