【问题标题】:R how get afinn score in a new columnR如何在新列中获得afinn分数
【发布时间】:2020-11-19 06:38:50
【问题描述】:

这是我的问题。 我想将与单个单词列相关的“AFINN”分数放入另一列 这是我的数据集

head(service_df)
# A tibble: 6 x 3
  word1   word2        n
  <chr>   <chr>    <int>
1 service delivery  8574
2 service covid     1163
3 service lockdown   541
4 service worker     389
5 service provider   370
6 service online     236

我想获得与“word2”列相关的分数,以便获得类似的东西

   word1   word2        n      AFINN_word_2
      <chr>   <chr>    <int>
    1 service delivery  8574    -1
    2 service covid     1163     0
    3 service lockdown   541     0  
    4 service worker     389     0.3
    5 service provider   370     0.1
    6 service online     236     1

(我只是将随机值放在 AFINN_word_2 下)

谢谢大家!

【问题讨论】:

    标签: r tidyverse sentiment-analysis


    【解决方案1】:

    很遗憾,您在示例中提供的所有记录均未与归因于 afinn 情绪评分的单词匹配。您可以尝试对 words2 中的单词进行标记或重新编码。

    # Install pacakges if they are not already installed: necessary_packages => vector
    necessary_packages <- c("tidytext", "tidyverse")
    
    # Create a vector containing the names of any packages needing installation:
    # new_pacakges => vector
    new_packages <- necessary_packages[!(necessary_packages %in%
                                           installed.packages()[, "Package"])]
    
    # If the vector has more than 0 values, install the new pacakges
    # (and their) associated dependencies:
    if(length(new_packages) > 0){install.packages(new_packages, dependencies = TRUE)}
    
    # Initialise the packages in the session: list of boolean => stdout (console)
    lapply(necessary_packages, require, character.only = TRUE)
    
    # data: service_df => data.frame
    service_df <- structure(list(word1 = c("service", "service", "service", "service", 
    "service", "service"), word2 = c("delivery", "covid", "lockdown", 
    "worker", "provider", "online"), n = c(8574L, 1163L, 541L, 389L, 
    370L, 236L)), class = "data.frame", row.names = c(NA, -6L))
    
    # Use afinn to get sentiment of word: res => data.frame
    res <- service_df %>%
      left_join(get_sentiments("afinn"), by = c("word2" = "word")) %>%
      select(word1, word2, n, AFINN_word_2 = value)
    

    【讨论】:

    • 我知道我显示的单词在 AFINN 词典中没有分数,但我还有很多其他单词没有显示...我想只为单词评分出现在词典中。
    • @ianux22 奇怪的数据子集选择然后给出。无论哪种方式,代码都是相同的。如果它符合您的要求,请投票并接受我的回答。
    • @ianux22 如果答案符合您的要求,通常是赞成和/或接受答案的好习惯。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 2011-11-10
    • 1970-01-01
    • 2015-02-03
    • 2021-05-19
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多