Recipe 14 Performing Setwise Operations on Friendship Data
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?”.
Use R setwise operations amd
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.
brooke_followers <- rtweet::get_followers("gbwanderson")brooke_friends <- rtweet::get_friends("gbwanderson")
Now we can see the count of mutual and disperate relationships:
# commonlength(intersect(brooke_followers$user_id, brooke_friends$user_id))
##  50
# difflength(setdiff(brooke_followers$user_id, brooke_friends$user_id))
##  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 rto see the impressive/diverse number of packages linking R to Redis
- Official Twitter API documentation on friends and followers.