title: R – Using a loop on a list of Twitter handles to extract tweets and create multiple data frames
body: <p>I’ve got a df that consists of Twitter handles that I wish to scrape on a regular basis.</p>
  
  <pre><code>df=data.frame(twitter_handles=c("@katyperry","@justinbieber","@Cristiano","@BarackObama"))
  </code></pre>
  
  <p><strong>My Methodology</strong></p>
  
  <p>I would like to run a <code>for</code> loop that loops over each of the handles in my df and creates multiple dataframes:</p>
  
  <p>1) By using the <code>rtweet</code> library, I would like to gather tweets using the <code>search_tweets</code> function.</p>
  
  <p>2) Then I would like to merge the new tweets to existing tweets for each dataframe, and then use the <code>unique</code> function to remove any duplicate tweets.</p>
  
  <p>3) For each dataframe, I'd like to add a column with the name of the Twitter handle used to obtain the data. For example: For the database of tweets obtained using the handle @BarackObama, I'd like an additional column called <code>Source</code> with the handle @BarackObama.</p>
  
  <p>4) In the event that the API returns 0 tweets, I would like Step 2) to be ignored. Very often, when the API returns 0 tweets, I get an error as it attempts to merge an empty dataframe with an existing one.</p>
  
  <p>5) Finally, I would like to save the results of each scrape to the different dataframe objects. The name of each dataframe object would be its Twitter handle, in lower case and without the <code>@</code></p>
  
  <p><strong>My Desired Output</strong></p>
  
  <p>My desired output would be 4 dataframes, <code>katyperry</code>, <code>justinbieber</code>, <code>cristiano</code> &amp; <code>barackobama</code>.</p>
  
  <p><strong>My Attempt</strong></p>
  
  <pre><code>library(rtweet)
  library(ROAuth)
  
  #Accessing Twitter API using my Twitter credentials
  
  key &lt;-"yKxxxxxxxxxxxxxxxxxxxxxxx"
  secret &lt;-"78EUxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  setup_twitter_oauth(key,secret)
  
  #Dataframe of Twitter handles    
  df=data.frame(twitter_handles=c("@katyperry","@justinbieber","@Cristiano","@BarackObama"))
  
  # Setting up the query
  query &lt;- as.character(df$twitter_handles)
  query &lt;- unlist(strsplit(query,","))
  tweets.dataframe = list()
  
  # Loop through the twitter handles &amp; store the results as individual dataframes
  for(i in 1:length(query)){
    result&lt;-search_tweets(query[i],n=10000,include_rts = FALSE)
    #Strip tweets that  contain RTs
    tweets.dataframe &lt;- c(tweets.dataframe,result)
    tweets.dataframe &lt;- unique(tweets.dataframe)
  }
  </code></pre>
  
  <p>However I have not been able to figure out how to include in my for loop the part which ignores the concatenation step if the API returns 0 tweets for a given handle. </p>
  
  <p>Also, my for loop does not return 4 dataframes in my environment, but stores the results as a <code>Large list</code></p>
  
  <p>I identified a <a href="https://stackoverflow.com/questions/34832171/using-a-loop-to-create-multiple-data-frames-in-r">post</a> that addresses a problem very similar to the one I face, but I find it difficult to adapt to my question.</p>
  
  <p>Your inputs would be greatly appreciated.</p>
  
  <p>Edit: I have added Step 3) in My Methodology, in case you are able to help with that too.</p>