title: Sentiment analysis (AFINN) in R
body: <p>I am trying to the sentiment of a dataset of Tweets using the AFINN dictionary (get_sentiments("afinn"). A sample of the dataset is provided below:   </p>
  
  <h1> </h1>
  
  <pre><code>A tibble: 10 x 2
     Date                TweetText                                                
     &lt;dttm&gt;              &lt;chr&gt;                                                    
   1 2018-02-10 21:58:19 "RT @RealSirTomJones: Still got the moves! That was a lo~
   2 2018-02-10 21:58:19 "Yass Tom \U0001f600 #snakehips still got it #TheVoiceUK"
   3 2018-02-10 21:58:19 Yasss tom he’s some chanter #TheVoiceUK #ItsNotUnusual   
   4 2018-02-10 21:58:20 #TheVoiceUK SIR TOM JONES...HE'S STILL HOT... AMAZING VO~
   5 2018-02-10 21:58:21 I wonder how many hips Tom Jones has been through? #TheV~
   6 2018-02-10 21:58:21 Tom Jones has still got it!!! #TheVoiceUK                
   7 2018-02-10 21:58:21 Good grief Tom Jones is amazing #TheVoiceuk              
   8 2018-02-10 21:58:21 RT @tonysheps: Sir Thomas Jones you’re a bloody legend #~
   9 2018-02-10 21:58:22 @ITV Tom Jones what a legend!!! ❤️ #StillGotIt #TheVoice~
  10 2018-02-10 21:58:22 "RT @RealSirTomJones: Still got the moves! That was a lo~
  </code></pre>
  
  <p>What I want to do is:
  1. Split up the Tweets into individual words.
  2. Score those words using the AFINN lexicon.
  3. Sum the score of all the words of each Tweet
  4. Return this sum into a new third column, so I can see the score per Tweet.</p>
  
  <p>For a similar lexicon I found the following code:</p>
  
  <pre><code># Initiate the scoreTopic
  scoreTopic &lt;- 0
  # Start a loop over the documents
  for (i in 1:length (myCorpus)) {
    # Store separate words in character vector
    terms &lt;- unlist(strsplit(myCorpus[[i]]$content, " "))
    # Determine the number of positive matches
    pos_matches &lt;- sum(terms %in% positive_words)
    # Determine the number of negative matches
    neg_matches &lt;- sum(terms %in% negative_words)
    # Store the difference in the results vector
    scoreTopic [i] &lt;- pos_matches - neg_matches
  } # End of the for loop
  
  dsMyTweets$score &lt;- scoreTopic
  </code></pre>
  
  <p>I am however not able to adjust this code to get it working with the afinn dictionary. </p>