【发布时间】:2018-05-16 18:09:26
【问题描述】:
我正在尝试基于 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 会出现在多行中。
这是我的尝试:
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$text 数据框中的单词并维护 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)
返回错误:
Error in strsplit(text, " ", fixed = TRUE) : non-character argument
我正在尝试转换为 tibble,因为这似乎是 tidytextmining 情绪分析所需的格式:http://tidytextmining.com/sentiment.html#the-sentiments-dataset
【问题讨论】:
-
为什么要转换tibble?换句话说,您的标题似乎并不代表实际问题。看来您只希望每个 url 有一个单词。我认为一种可能的 tibbliverse 方法可能是
res %>% group_by(url) %>% transform(text = strsplit(text, " ", fixed = TRUE)) %>% unnest() %>% count(url, text)(假设text是一个字符串而不是一个因素) -
@DavidArenburg 请查看更新
标签: r dataframe tibble tidytext