使用字数将数据框转换为 tibble
Converting data frame to tibble with word count
我正在尝试根据 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)
其中 returns :
# A tibble: 2 x 2
res.text n
<fctr> <int>
1 this is test text1 1
2 this is test text2 1
如何计算 res$text dataframe 中的单词并维护 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)
returns 错误:
Error in strsplit(text, " ", fixed = TRUE) : non-character argument
我正在尝试转换为 tibble,因为这似乎是 tidytextmining 情感分析所需的格式:http://tidytextmining.com/sentiment.html#the-sentiments-dataset
您正在寻找这样的东西吗?当你想用 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
我正在尝试根据 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)
其中 returns :
# A tibble: 2 x 2
res.text n
<fctr> <int>
1 this is test text1 1
2 this is test text2 1
如何计算 res$text dataframe 中的单词并维护 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)
returns 错误:
Error in strsplit(text, " ", fixed = TRUE) : non-character argument
我正在尝试转换为 tibble,因为这似乎是 tidytextmining 情感分析所需的格式:http://tidytextmining.com/sentiment.html#the-sentiments-dataset
您正在寻找这样的东西吗?当你想用 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