Я провожу очень простой анализ настроений на довольно большом наборе данных, который продолжает расти с каждым днем. Мне нужно передать эти данные в блестящее приложение, где я могу настроить диапазон дат. Вместо того, чтобы проводить анализ снова и снова, я бы хотел создать новый CSV с суммой каждой оценки тональности по дате. Однако у меня проблемы с повторением даты. Вот несколько примеров данных и выражение lapply()
, которое я пробовал, но он не работает.
library(tidyverse)
library(syuzhet)
library(data.table)
df <- data.frame(date = c("2021-01-18", "2021-01-18", "2021-01-18", "2021-01-17","2021-01-17", "2021-01-16", "2021-01-15", "2021-01-15", "2021-01-15"),
text = c("Some text here", "More text", "Some other words", "Just making this up", "as I go along", "hope the example helps", "thank you in advance", "I appreciate the help", "the end"))
> df
date text
1 2021-01-18 Some text here
2 2021-01-18 More text
3 2021-01-18 Some other words
4 2021-01-17 Just making this up
5 2021-01-17 as I go along
6 2021-01-16 hope the example helps
7 2021-01-15 thank you in advance
8 2021-01-15 I appreciate the help
9 2021-01-15 the end
dates_scores_df <- lapply(df, function(i){
data <- df %>%
# Filter to the unique date
filter(date == unique(df$date[i]))
# Sentiment Analysis for each date
sentiment_data <- get_nrc_sentiment(df$text)
# Convert to df
score_df <- data.frame(sentiment_data[,])
# Transpose the data frame and adjust column names
daily_sentiment_data <- transpose(score_df)
colnames(daily_sentiment_data) <- rownames(score_df)
# Add a date column
daily_sentiment_data$date <- df$date[i]
})
sentiment_scores_by_date <- do.call("rbind.data.frame", dates_scores_df)
Я хотел бы получить что-то вроде этого (данные здесь составлены и не будут соответствовать приведенному выше примеру)
date anger anticipation disgust fear joy sadness surprise trust negative positive
2021-01-18 1 2 0 1 2 0 2 1 1 2
2021-01-17 1 2 0 2 3 3 1 2 0 1