2017-12-02 15 views
1

http://tidytextmining.com/sentiment.html#the-sentiments-dataset을 기반으로 정서 분석을 시도하고 있습니다. 정서 분석을 수행하기 전에 데이터 세트를 깔끔한 형식으로 변환해야합니다.단어 개수로 데이터 프레임 변환하기

내 데이터 집합 형식이다 : 행 당 하나 명의 관찰로 변환하기 위해

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이 여러 행에 나타납니다.

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

어떻게 고해상도 $ 텍스트 dataframe 단어를 계산하고 감정 분석을 수행하기 위해 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) 

: 여기

내 시도?

업데이트 :

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을 원한다고 생각합니다. %% unnest() %> % count (url, text);}}}} 가능한 한 티브 (tibbliverse) 접근법은'res %> % group_by (url) %> % transform (text = strsplit (text, "고정 = TRUE))'('text'는 문자열이고 인자가 아니라고 가정) –

+0

@DavidArenburg 업데이트를 참조하십시오 –

답변

4

위치 : 나는이 감정 분석을 tidytextmining에 필요한 형식으로 나타나는 tibble로 변환을 시도하고있어

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

이런 식으로 찾고 계신가요? tidytext 패키지로 정서 분석을 처리하려면 각 문자열의 단어를 unnest_tokens()으로 구분해야합니다. 이 기능은 텍스트를 단어로 분리하는 것 이상의 기능을 수행 할 수 있습니다. 나중에 기능을 보길 원한다면. 한 줄에 단어가 생기면 count()을 사용하여 각 단어가 각 텍스트에 몇 번 나타날지 계산할 수 있습니다. 그런 다음 중지 단어를 제거하려고합니다. tidytext 패키지에는 데이터가 있으므로 호출 할 수 있습니다. 마지막으로 정서 정보가 필요합니다. 여기에서는 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