Tavish Srivastava — July 3, 2014
Big data Business Analytics Data Exploration Intermediate R Social Media Technique

Facebook has huge data bank and it allows us to make use of it to some extent.

October is a month of celebration in India. We have festivals like Diwali and Dushehra in October, which makes the entire month a time to celebrate and reunion. Every time we meet our friends and relatives at different places, to make it easier for everyone to reunite.  Every time before going to the city, I update my FB/Twitter status to “Going to city xyz” and get a bunch of replies from people who are traveling to the same place. But almost every time I miss some of my friends just because of lack of information. This time I went a step ahead and connected my FB account to R and looked for people who have their current city as my target location. Surprisingly, I got 10 more friends who were in the city, whom I might have missed if this exercise was never done. In this process, I had a lot of fun with other user profile variables FB permits us to look at. In this article, I will help readers to understand this process of connecting FB to R and demonstrate the simplicity of the process. This type of analysis is not restricted only to the case in hand but a much broader set of daily life problems.

We will use library Rfacebook for this analysis.

graph_social_networking

 How to connect R to Facebook  

Facebook provides you two simple ways to import data from the website. I will demonstrate the simpler one in this article.

Step 1 : Goto the link ” https://developers.facebook.com/tools/explorer/” . This will open thee FB developer page.

fb

Step 2: Change the API Version(Red box in the picture) to “unversioned”

Step 3: Click the “Get Access Token” (Green box in the picture).

permissions

Step 4: Check all the boxes in all three tabs. These are the permissions you are asking from yourself to access. Assuming you do not wish to hide anything from yourself, you can safely check all boxes.

Step 5 : Click on the button “Get Access Token”. This token is valid for 2 hours.

Step 6 : Store your token as variable in R studio. You can use the following code for the same :

> token <- "XXXXX12333YYY"
> me <- getUsers("me", token=token)
> me$name

[1] “Tavish Srivastava”

Now, you have facebook connected to your R session for the next 2 hours.

 Search people in a particular city among your friend list 

I and all my relatives decided to meet in Pune (Maharashtra) this year and hence “Pune” is the location I am looking for in the current location field of all my friends profile. Imagine doing the same thing manually on facebook. Let’s take a smarter route and check out the frequency distribution of current location among the user IDs in my friend list. To accomplish this task you can execute a simple code on R.

Step1 : Pull out the list of all friends and their ID.

Step 2 : Pull all the user details corresponding to this table of IDs.

Step 3 : Check the frequency distribution of all current location. This is done to make sure the same name “Pune” is not appearing in different formats.

This frequency distribution is a reason why this method adds power over traditional search on Facebook. For example, if we were meeting in Delhi, I would want to search Delhi, Gurgaon, Noida and possibly Faridabad for my friends. However, through this method, I can write one single query to get it.

You can use following code to do the same :

> my_friends <- getFriends(token, simplify=TRUE)
> my_friends_info <- getUsers(my_friends$id, token=token, private_info=TRUE)
> table(my_friends_info$location)

pune

We see that, I have 16 friends with their current location as Pune. I also get the exact string I should search for to complete my task. Following is the code you can use to find these 16 friends.

> Pune_resident <- subset(my_friends_info$first_name,my_friends_info$location == "Pune, Maharashtra")
> Pune_resident

Finally I get the list of names of my friends, who have their current location as Pune. While doing this exercise, I found some other interesting facts about my friend list. It is very easy to tabulate the relationship_status of all your friends. Because the possible values are very few, it becomes interesting to analyze the same. Following is a code I used to tabulate the relationship_status of my friends.

> table(my_friends_info$relationship_status)

 Engaged : 3

 In a relationship : 6

 It’s complicated    : 5

 Married  : 126

 Single : 434

As I have been lately busy in my work, I completely lost track of people getting engaged. Here is an easy method to find the same :

> engaged.friends <- subset(my_friends_info$first_name,my_friends_info$relationship_status == "Engaged")
> engaged.friends

I did a tabulation on each of the user information Facebook shared and discovered new things about my friends every single time.

 End Notes 

I found this small piece of analysis both interesting and insightful. It just helps you get a summary of everything. You can go through the user information of your entire friend list in less than 5 minutes. You can use this data to visualize your friends on a graph and see various clusters of population (Hint – you will need to use igraph library for this). You can do some cool things like define the distance between nodes basis interactions on Facebook and see which are the closest people to you as per Facebook.

How would you play around with your social media data? Have you done such small experiments on your Facebook or twitter profile? Did you go beyond the scope of this article in your analysis? Please share with us your thoughts on the topic.

If you like what you just read & want to continue your analytics learningsubscribe to our emailsfollow us on twitter or like our facebook page.

 

About the Author

Tavish Srivastava

Tavish Srivastava, co-founder and Chief Strategy Officer of Analytics Vidhya, is an IIT Madras graduate and a passionate data-science professional with 8+ years of diverse experience in markets including the US, India and Singapore, domains including Digital Acquisitions, Customer Servicing and Customer Management, and industry including Retail Banking, Credit Cards and Insurance. He is fascinated by the idea of artificial intelligence inspired by human intelligence and enjoys every discussion, theory or even movie related to this idea.

Our Top Authors

  • Analytics Vidhya
  • Guest Blog
  • Tavish Srivastava
  • Aishwarya Singh
  • Aniruddha Bhandari
  • Abhishek Sharma
  • Aarshay Jain

Download Analytics Vidhya App for the Latest blog/Article

47 thoughts on "Using Facebook as an analyst (Hint – using R)"

Gaurav
Gaurav says: July 03, 2014 at 4:56 am
Hi Tavish, Thanks for the nicely explained article. As you mentioned that token is valid for 2 hours. So what happens to the data post the expiry of 2 hours. Also, can one extract entire fb data or only corresponding to one's friendlist. regards & care, Gaurav Reply
Ravi Kamani
Ravi Kamani says: July 03, 2014 at 5:55 am
Hi Tavish, Nice short sweet and most importantly concise and graphical article on FB . Similarly we can mine the tweets from twitter to get the sentiment on a particular person,product,party,celebrity etc. Reply
Nidhish Bhatia
Nidhish Bhatia says: July 03, 2014 at 6:00 am
very interesting Reply
Tavish
Tavish says: July 03, 2014 at 9:30 am
Thanks Ravi. We will also cover twitter mining and sentiment analysis in our coming articles. Do share with us any insights or analysis you have done on FB & twitter mining. Reply
Avinash
Avinash says: July 03, 2014 at 10:45 am
Hi Tavish, Really impressed!! This sounds interesting for a nascent learner of R (Like me) Kudos mate. I am trying to execute the steps specified here and i am facing a problem while getting the location list of the users in my profile, i was shown only 5 people across 3 locations, can you please help me out on this. Also i went through the Rfacebook package and was trying different codes like getLikes, getCheckins etc. I came across a function called "fbOAuth" which says , it creates a long-lived OAuth access token that enables R to make authenticated calls tothe Facebook API. Can you throw some light on this . Thank You Reply
Vani P
Vani P says: July 03, 2014 at 11:56 am
Hi Tavish, Nice way of mining and analyzing the data from FB using R. Can we do the same kind of analysis using SAS? Reply
santhosh
santhosh says: July 03, 2014 at 1:01 pm
Clear and Concise. Thanks for the article Tavish! I have recently started reading AV blogs and i feel very lucky to land to such a magnificent website on Analytics. Reply
Ravi Sankar
Ravi Sankar says: July 03, 2014 at 5:55 pm
Hi Tavish, I tried above example but couldn't able to get few variables like "location" , "relationship_status" and "hometown"etc. In all the places i am able to see only "NA" values, even though i am able to see those values in facebook correctly. Can you help me if i got wrong somewhere? Reply
Vikesh
Vikesh says: July 03, 2014 at 7:03 pm
Hi, I tried using this but strangely getUsers function fetches information of only one of my friend however I have 400+ friends in fb. could there be somthng wrong with my fb settings? Reply
njn Long 1
njn Long 1 says: July 04, 2014 at 3:00 am
Great sharing. Maybe possible to use fan page to do similar analysis? Reply
Ajay
Ajay says: July 04, 2014 at 2:37 pm
This is a very well written and simple to follow tutorial so great work by the writer. I had written something differnt but using the same package at http://decisionstats.com/2014/05/10/analyzing-facebook-networks-using-rstats/ but your example is very nice and logical. Great job! Reply
Rahul
Rahul says: July 05, 2014 at 4:02 am
Hi Tavish, Very Nice tutorial. Only Missing thing is loading Rfacebook packages i.e. "> library("Rfacebook")" and offcourse its dependent packages. Good Job. Hi Ajay, Nice link http://decisionstats.com/ Reply
Saurabh Singh
Saurabh Singh says: July 06, 2014 at 6:56 pm
Really Nice Article.Simple & easy to understand. Thanks for this and keep up the Good work and share your knowledge with us. Thanks Reply
Dhanasekaran
Dhanasekaran says: July 06, 2014 at 9:00 pm
Hi Tavish, Thanks for this article. I have tried this for my FB profile and it was interesting. Reply
mayank soni
mayank soni says: July 07, 2014 at 1:55 am
I am quite sure you must be aware but there is another way to know the friends in your city or to run any other query for that matter. You have to use the facebook's graphic interface for this - Simply type in the search box - "People who live in Bangalore" Reply
Om
Om says: July 07, 2014 at 5:11 am
has anyone got this error while using Rfacebook package: Error in function (type, msg, asError = TRUE) : Could not resolve host: graph.facebook.com; Host not found Reply
Rajendra
Rajendra says: July 07, 2014 at 5:25 am
Hi Tavish, Good one to understand easily. I tried, but I did not get access code? Where does it display? Do I need to change my account to public view? However I went to debug mode and choose the ID column = Expires =1404716400 (in about an hour) assuming that this is an access token. when I run R command using this token I am getting the below issue. "Error: could not find function "getUsers"" Please help me.. Regards Raj Reply
Tavish
Tavish says: July 07, 2014 at 3:26 pm
Avinash, The limited list you are getting is because of your facebook settings. Check what restrictions have you added for any application to access your profile. fbOAuth is another way to access your network. Here you will create a dummy application on facebook which will redirect your entire facebook data to localhost. You can check out detailed instructions in CRAN website. Let me know in case you face any challenge. Tavish Reply
Tavish
Tavish says: July 07, 2014 at 3:26 pm
Yes Vani, Using SAS it is possible. However, it is not this easy. Tavish Reply
Tavish
Tavish says: July 07, 2014 at 3:27 pm
Thanks Santosh. Reply
Tavish
Tavish says: July 07, 2014 at 3:29 pm
Ravi, This looks like a permission issue. You can check this out using any of your friends FB token. Tavish Reply
Tavish
Tavish says: July 07, 2014 at 3:30 pm
Vikesh, Again this is a App setting issue. Try using someone else's account token. Tavish Reply
Tavish
Tavish says: July 07, 2014 at 6:05 pm
Can you elaborate on what exactly are you asking. Reply
Tavish
Tavish says: July 07, 2014 at 6:07 pm
Om, My sense is that that you did not install Rfacebook package before calling it as a library. Reply
Tavish
Tavish says: July 07, 2014 at 6:09 pm
Raj, The 2 hours token can be achieved using the link given in the article. You can also refer to CRAN website on Rfacebook to get a even better view on this procedure. Reply
Venkat
Venkat says: July 10, 2014 at 2:39 pm
Hi Tavish, Is there any way i can change the app settings? getfriends returns only details of 7 friends. Reply
Nobel
Nobel says: July 11, 2014 at 6:37 am
why i am getting this error; could not find function "getUsers" Reply
Om
Om says: July 11, 2014 at 12:32 pm
Tavish, I have installed Rfacebook with other dependent packages but still getting this error. Reply
Pravin
Pravin says: July 17, 2014 at 7:50 am
Hello Tavish, I tried it, but getting below error Error: could not find function "getUsers" Please help me. Regards, Pravin. Reply
Siddharth Dubey
Siddharth Dubey says: July 23, 2014 at 7:01 pm
Hi…on running this code “table(my_friends_info$location)”, I am getting a statement saying “table of extent 0 >”. Can you please tell me what I am doing wrong? I have checked all the boxes, I believe there is some problem when I am trying to run “my_friends_info <- getUsers(my_friends$id, token=token, private_info=TRUE)" coz in this case when I print the "my_friends_info" dataset, I get a table with only NA values. Please help. Reply
Tavish Srivastava
Tavish Srivastava says: July 23, 2014 at 7:42 pm
Looks like your library is not loaded. Reply
Tavish Srivastava
Tavish Srivastava says: July 23, 2014 at 7:44 pm
Try the code with some other's facebook token. If it works probably its an issue with your FB settings Reply
shalli
shalli says: August 04, 2014 at 1:17 pm
very good one. Reply
Aurindam dhar
Aurindam dhar says: August 04, 2014 at 4:17 pm
Hi, while loading Rfacebook (after downloading it), it's saying that you need to install 'httr' package..But I am not able to download the same. Is it available in the open R version or available for enterprise version only?? Reply
rathankar
rathankar says: October 11, 2014 at 11:22 am
load the library using the command library("Rook") and library("Rfacebook"). there are certain dependancies like brew, httr, json and tools that needs to be added along with them. just check whether they are also loaded along with it Reply
nitin
nitin says: October 30, 2014 at 9:14 am
Hi Tavish, Executing the command to get the friend list using the function getFriends, I am only able to extract 26 friends in my friend list, though I have more than 900 friends. Can you please let me know what is the issue here? Reply
Tavish
Tavish says: October 30, 2014 at 11:21 am
This is possibly a case of security settings on your FB. Reply
Tavish
Tavish says: October 30, 2014 at 11:23 am
Aurindham, You can try placing this package directly to your R library folder after downloading it from CRAN website archive folder. Tavish Reply
Olalekan Elesin
Olalekan Elesin says: November 23, 2014 at 3:02 pm
Thanks Tavish. great post. One problem: the getFriends() function only returns one row. What could be the problem? Reply
Palash
Palash says: January 22, 2015 at 12:55 pm
Hi Tavish, Great work here! However, when I ran this, I could only find info on those people who had used the facebook developers api before. (on using getUsers function) http://127.0.0.1:29306/library/Rfacebook/html/getFriends.html You could see here that the updated Graph 2.0 API restricts people to only those people's info who have used the developers app before. Is there a way around this? Reply
sandesh
sandesh says: March 02, 2015 at 6:01 pm
Great job Tavish, we have a small provate facebook group and I have been given an admin right to this group so that I can do some data mining. After doing some research and with my limited R knowledge decided to use RFacebook package. But it appears that after the introduction of version 2.0 of the Graph API, only friends/groups who are using the APPLICATION that I used to generate the token to query the API will be returned. I know in order to get the token ( for short term seession) or App_id and App_secret ( for long term term use) we need to create an app. Now, my question is can I allow this app to the group that I admin? If so, how do I do this? Thank you sandesh Reply
Varun Yadav
Varun Yadav says: April 04, 2015 at 10:21 am
Hi Tavish Well I am ardent reader of Analytics Vidhya. I started following AV since last month. You won't believe I read almost every article just in one week. It helped me lot to improve my analytics skills. Keep Going My best wishes are with you Guys. BTW I followed the procedure to fetch the data of friends on facebook. But i got only 1 friend. So i checked Permissions clause of facebook and found following 1 ) A user access token with user_friends permission is required to view the current person's friends. 2 ) This will only return any friends who have used (via Facebook Login) the app making the request. 3 ) If a friend of the person declines the user_friends permission, that friend will not show up in the friend list for this person. So It is clear that i need permission to access my friend's data. How can i do that is there any alternate way to fetch data. Reply
Mickael LENOURY
Mickael LENOURY says: June 05, 2015 at 12:25 am
I think it's not available now, need to scrape the Facebook page . Reply
Neha Gupta
Neha Gupta says: June 22, 2015 at 7:07 am
hey...when I tried to run the code....it displays the following error: "Only friends who use the application will be returned See ?getFriends for more details Error in getFriends(token, simplify = TRUE) : No friend information is available. See ?getFriends for more details." Is it really necessary that friends only who made application can be accessed? Reply
Rajesh Rajamani
Rajesh Rajamani says: September 22, 2015 at 12:49 pm
Hello, Unfortunately with the Graph API Explorer upgrade to version 2.4 since May 2015 , this is pretty much deprecated. I rely on getting the user IDs in by looping through the comments made by users ( and then scrape the user information available by the virtue of the user IDs that I get ). But again when we get the user details it is very minimal and we will get to know only those pieces of information that the user has consented to keep public in facebook profile privacy settings. Reply
Lukmanul Hakim
Lukmanul Hakim says: October 11, 2015 at 10:34 pm
hi.,.i really interesting about facebook mining but i have problm about it. when i was running in r i get the information error. can you help me? > my_friends <- getFriends(access_token, simplify=TRUE) Only friends who use the application will be returned See ?getFriends for more details Error in getFriends(access_token, simplify = TRUE) : No friend information is available. See ?getFriends for more details. Reply
Vishal Gheware
Vishal Gheware says: December 12, 2016 at 12:31 pm
Awesome! Crisp and clear explaination with practical example. By the way now it doesn't allow unversioned API selection so just retained v2.8 and it went well. Reply

Leave a Reply Your email address will not be published. Required fields are marked *