我試圖執行基於http://tidytextmining.com/sentiment.html#the-sentiments-dataset情感分析。在執行情感分析之前,我需要將我的數據集轉換爲整潔的格式。轉換數據幀與字tibble算
我的數據集的形式:
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新列。相同的網址將出現在多行中。
這裏是我的嘗試:
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 $文本數據幀的話,爲了進行情感分析維持網址是什麼?
更新:
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)
返回錯誤:
Error in strsplit(text, " ", fixed = TRUE) : non-character argument
我試圖轉換爲tibble,因爲這似乎是tidytextmining情感分析所需的格式:http://tidytextmining.com/sentiment.html#the-sentiments-dataset
爲什麼你需要將其轉換tibble?換句話說,你的頭銜似乎並不代表真正的問題。看來你只是想要一個字可以按每個網址。我認爲,一個可能的tibbliverse方法可能是'水庫%>%GROUP_BY(URL)%>%轉化(文字= strsplit(文字 「」 固定= TRUE))%>%UNNEST()%>%計(網址,文本)'(假設'text'是一個字符串,而不是一個因素) –
@DavidArenburg請參閱更新 –