Cleaning your Twitter account with R

July 2, 2021
rtweet rstats

In March 2020, I decided to take a break from Twitter. I started my account in 2014 and for six years I used it in fits and bursts in somewhat inconsistent fashion – I followed an eclectic group of accounts consisting of musicians, academics, joke accounts, reporters, politicians, and political activists. This turned my timeline into a nice stream of constant amorphous distraction with a large dose of doomscrolling. Once the pandemic hit, my Twitter feed transformed from a moderate waste of time into a source of existential dread. The deluge of information and hot takes was just too much for me, so I entered an extremely regulated information diet, locked my Twitter account and stayed off the site for most of 2020.

Over the last few months, I have been lurking more and more on Twitter. When used correctly it can be a useful source of information, helping you stay updated in current research trends and for people who use R, it can be an invaluable resource for learning about new packages and helpful tutorials. However, while lurking, I’d also see absurd things like two academics arguing on Twitter before devolving into comparing h-index rankings. Through this lurking I have mentally drawn up what I want to get out of Twitter and what I want to avoid. I want to learn about new research in topics that I am interested in and keep my R skills up to date. Everything else (so most of Twitter) I want to avoid. Since I wanted a fresh start, I decided to delete all of my old tweets and liked tweets.

How to delete all your tweets using R

Twitter doesn’t have a feature to easily do this on the website itself, but luckily the rtweet package makes it easy to access Twitter’s API so that you can delete all of your tweets and likes through R. Since I am a bit of data hoarder, I want to make sure that I had backups of my tweets before I delete them. I will most likely never look at these again, but who knows, maybe in five years I’ll really want to seem my hot takes about the 2016 election.

First I am going to grab my user data using lookup_users. This is a bit redundant because I am also going to back up my entire timeline. When you use lookup_users you are just grabbing the most recent tweet which has user data attached to it.

jdn_data <- lookup_users("JoeDNoonan") 

### Saving in different formats.
write_as_csv(jdn_data, "jdn_user_data.csv")
write_xlsx(jdn_data, "jdn_data.xlsx")

You’ll notice that I am using write_as_csv and not write_csv This is because there are some variables that are nested lists. This doesn’t work well with the flat structure of .csv files, but luckily the rtweets package has you covered with the write_as_csv function which takes care of this for you. You can also use write_xlsxif you prefer to work with excel sheets.

Next it is time to backup my timeline using get_timelines. Because of rate limiting, you can only grab 3,200 tweets at a time. Luckily, I never tweeted that much so I was under the limit. If you have more than 3,200, you’ll have to set up a loop that runs the function in fifteen minute intervals – if you have a really high number of Tweets this might take a very long time!

### Grab your timeline 
jdn_timeline <- get_timelines("JoeDNoonan", n = 3200)

### Save your timeline 
write_as_csv(jdn_timeline, "jdn_timeline.csv")
write_xlsx(jdn_timeline, "jdn_timeline.xlsx")

Next, I am grabbing all of my likes. The procedure is the same but get_favorites, but the amount of likes we can grab is lower.

jdn_likes <- get_favorites("JoeDNoonan", n = 3000)

### Save all your likes 
write_as_csv(jdn_likes, "jdn_likes.csv")
write_xlsx(jdn_likes, "jdn_likes.xlsx")

Now it is time for the big moment – deleting every one of my old tweets. First, I am creating a list of all of my Tweet IDs that I am pulling from the jdn_timeline dataframe. This is what we will iterate through when I write my delete for loop.

### Create a list of all of your tweets! 
tweet_ids <- jdn_timeline$status_id

To delete a tweet you use post_tweet, but you fill in the destroy_id parameter. Instead of posting a tweet this deletes (or the destroys as they dramatically say) an existing tweet.

### You can delete a tweet using the destory_id param.
post_tweet(destroy_id = tweet_ids[1])

Next, I am just going loop through this procedure, iterating through the list of my Tweet ids.

### Write a little for loop to do to every tweet. 
for (i in seq_along(tweet_ids)) {
  message("Deleting Tweet ", i, " of ", length(tweet_ids))
   post_tweet(destroy_id = tweet_ids[i])}

For favorites, the procedure is generally the same, starting with cereating a list of the tweet IDs for all of the likes.

### Create a list of all your likes
like_ids <-jdn_likes$status_id

The syntax for the post_favorite works a bit differently. Instead of having a parameter called destroy_id like post_tweet, it has a parameter violently called destory which you set to TRUE. The loop works in the same way as the previous loop.

post_favorite(like_ids[1],  destroy = TRUE)

for (i in seq_along(like_ids)) {
  message("Deleting Fav ", i, " of ", length(like_ids))
  post_favorite(like_ids[i],
                destroy = TRUE)}

And that is it. A free and relatively quick way to clear out your Twitter history. If you are not interested in deleting everything, you could easily filter for only a specific time span or whatever other parameters based on the information you get from the get_timelines function.

Automate PowerPoint Production Using R

February 17, 2021
officer powerpoint DataViz rstats

Who is in the UN's Twitter network?

January 25, 2021
rtweet rstats