0

Here is my problem. I want to put the "AFINN" score related to a column of single words into another column Here my dataset

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

I want to obtain the score related to the column "word2" in order to have something like this

   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

(I just put random values under AFINN_word_2)

Thanks Everyone!

1 Answer 1

0

Unfortunately for you none of the records you provided in sample match with words ascribed an afinn sentiment score. You could try tokenising or recoding the words you have in 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)
3
  • I know the words I showed has no score in the AFINN lexion, but I have a lot of other words that I didn't show... I would like to have the score just for the words that are present in the lexicon.
    – ianux22
    Commented Nov 19, 2020 at 21:14
  • @ianux22 strange subset of data to chose to give out then. The code is the same either way. Upvote and accept my answer if it does what you require. Commented Nov 20, 2020 at 0:29
  • @ianux22 It is generally good practice to upvote and/or accept an answer if it does what you require. Commented Jan 13, 2021 at 2:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.