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 and 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.

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

## [1] 50
## [1] 226

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.