我试图执行基于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请参阅更新 –