Recipe 14 Performing Setwise Operations on Friendship Data

14.1 Problem

You want to operate on collections of friends and followers to answer questions such as “Who isn’t following me back?”, “Who are my mutual friends?”, and “What friends/followers do certain users have in common?”.

14.2 Solution

Use R setwise operations amd rtweet::lookup_friendships().

14.3 Discussion

R has set operations and they’ll do just fine for helping us cook this recipe.

If you need a refresher on set operations, check out this introductory lesson from Khan Academy.

library(rtweet)
library(tidyverse)
brooke_followers <- rtweet::get_followers("gbwanderson")
brooke_friends <- rtweet::get_friends("gbwanderson")

Now we can see the count of mutual and disperate relationships:

# common
length(intersect(brooke_followers$user_id, brooke_friends$user_id))
## [1] 50
# diff
length(setdiff(brooke_followers$user_id, brooke_friends$user_id))
## [1] 206

The Python counterpart to this cookbook suggests Redis as a “big-ish” data solution for performing set operations at-scale. R has at least 3 packages that provide direct support for Redis, so if you need to perform these operations at-scale, cache the info you retrieve from the Twitter API into Redis and then go crazy!

14.4 See Also

  • Google (yes, seriously) redis packages r to see the impressive/diverse number of packages linking R to Redis
  • Official Twitter API documentation on friends and followers.