Data can tell you terrific things. The best things. But let me tell you – you gotta have great tools. Believe me. People say to me, they say “wow, how is your data analysis so great?” – they can’t believe it. These people – they don’t know how to do it. They don’t know how to win. I will teach them how to win. With R.

Social media is a terrific source of data for tracking cultural trends and opinions. Here, we’ll look at some Apprentice-level examples of Twitter Analytics.

To illustrate simple social media analytics, we’ll consider one twitter account that posts a HUGE number of tweets using very simple, repetitive language.

A word of warning – this account is often incoherent, rambling and offensive. It is probably a cheap Russian chat-bot designed to scam you. Don’t trust it!

For this tutorial you’ll need:

We’ll get started by downloading the tweet history of our russian chatbot @realDonaldTrump.



Our tt object is a data-frame (a table) with several columns of useful information:

  • Tweet text is stored in the “text” column.
  • The date and time is stored in the “created” column (in UTC timezone)
  • retweetCount – popularity of the tweet.


Next we’ll convert the default twitter timezone (UTC) to Eastern Standard Time. Then we’ll extract individual date and time from the datetime column. To do the timezone conversions, we’ll make use of the POSIXct class for storing date/time data.


Great. Now we have some extra information in our tweet table:

  • tt$Date: The date it was created
  • tt$wday: The day of the week it was created
  • tt$EST: The time that it was created – EST timezone (with date fixed to simplify plotting of time)
  • tt$ESThour: The hour it was created (with date fixed to simplify plotting of time)


Next, we’ll define a simple function that tallies up tweets per hour and plots the result:



Our twitterbot is known to be heavily influenced by FoxNews. Here’s the daily schedule of FoxNews:


Ok, lets use our plotTweets function to plot tweet tallies for pre-inauguration (Jan 20, 2017) and post-inauguration.



The results:

Trump Twitter analytics

Trump’s tweets before and after inauguration. Arranged according to the Fox News program schedule.



We see that the hourly output of our tweeting twitterbot has changed considerably since Jan 20!