-4

I have collected tweets from a single user:

api_key <- "XXXX"
api_secret <- "XXXX"
access_token <- "XXXX"
access_token_secret <- "XXXX"
setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)

salvini <- rtweet::get_timeline(user = "matteosalvinimi", n = 3600)

From here, I only know how many likes or retweets each tweet has received (respectively through salvini$favourite_count and salvini$retweet_count). What I would like to do is collecting the text of each of the comments to these tweets.

Does anyone know how to do it?

1 Answers1

2

First, please review the protocol for asking questions. You got downgraded because you (1) did not provide a reproducible data set and (2) asked a question answered elsewhere here.

Here is a quick answer though:

library(twitteR);library(dplyr); library(ROAuth)
#set API Keys; to obtain, go here: https://apps.twitter.com/ and make an application for  your twitter account

api_key <- "paste yours here"      # create a set of 'keys' & 'tokens'
api_secret <- "paste yours here"
access_token <- "paste yours here"
access_token_secret <- "paste yours here"
setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)

#grab latest tweet data
tweets1 <- searchTwitter('@oprah', n=1000)
TweetsBy1<-twListToDF(tweets1)  #convert to dataframe
TweetsBy1$account<-"Oprah"  # useful to have this

glimpse(TweetsBy1) # look at your data; the text variable is what you're after

temp<- TweetsBy1 %>% 
        group_by(created) %>%   # you will need to reformat this date variable
        summarise(numTweets=n())

TweetsBy1$text   # this is the text of the tweets

ggplot(temp, aes(created,numTweets))+geom_bar(stat="identity")+
       theme_bw()+ylab("Number of Tweets")+
       ggtitle("Number of Tweets by Date")

As for text analysis, that's a whole other ball of wax. See the tidytext package for more info.

Ben
  • 1,113
  • 10
  • 26
  • Hi Ben, thank you for your points. I have inserted the code I have used to collect the data. My question does not seem to have appeared in other posts: if I got it right, in your syntax you obtain tweets including the word "@oprah" (which might be interpreted as posts about oprah), and with `TweetsBy1$text` you extract the text. What I meant was collecting the text commenting each of the tweets posted by user "matteosalvinimi". I hope this is clearer now. – Luca Carbone Nov 01 '18 at 21:48
  • @LucaCarbone This answer might help you: https://stackoverflow.com/questions/47226073/r-find-all-replies-to-a-users-tweets-from-their-follower-list – Ben Nov 05 '18 at 17:03