Using Facebook as an analyst (Hint – using R)

Tavish Srivastava 17 Apr, 2015 • 4 min read

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

[stextbox id=”section”]  How to connect R to Facebook  [/stextbox]

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 :

[stextbox id=”grey”]

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

[1] “Tavish Srivastava”

[/stextbox]

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

[stextbox id=”section”]  Search people in a particular city among your friend list  [/stextbox]

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 :

[stextbox id=”grey”]

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

[/stextbox]

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.

[stextbox id=”grey”]

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

[/stextbox]

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.

[stextbox id=”grey”]

> table(my_friends_info$relationship_status)

 Engaged : 3

 In a relationship : 6

 It’s complicated    : 5

 Married  : 126

 Single : 434

[/stextbox]

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 :

[stextbox id=”grey”]

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

[/stextbox]

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

[stextbox id=”section”]  End Notes [/stextbox]

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.

 

Tavish Srivastava 17 Apr 2015

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.

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers

Clear

Gaurav
Gaurav 03 Jul, 2014

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

Ravi Kamani
Ravi Kamani 03 Jul, 2014

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.

Nidhish Bhatia
Nidhish Bhatia 03 Jul, 2014

very interesting

Tavish
Tavish 03 Jul, 2014

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.

Avinash
Avinash 03 Jul, 2014

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

Vani P
Vani P 03 Jul, 2014

Hi Tavish, Nice way of mining and analyzing the data from FB using R. Can we do the same kind of analysis using SAS?

santhosh
santhosh 03 Jul, 2014

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.

Ravi Sankar
Ravi Sankar 03 Jul, 2014

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?

Vikesh
Vikesh 03 Jul, 2014

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?

njn Long 1
njn Long 1 04 Jul, 2014

Great sharing. Maybe possible to use fan page to do similar analysis?

Ajay
Ajay 04 Jul, 2014

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!

Rahul
Rahul 05 Jul, 2014

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/

Saurabh Singh
Saurabh Singh 06 Jul, 2014

Really Nice Article.Simple & easy to understand. Thanks for this and keep up the Good work and share your knowledge with us. Thanks

Dhanasekaran
Dhanasekaran 06 Jul, 2014

Hi Tavish, Thanks for this article. I have tried this for my FB profile and it was interesting.

mayank soni
mayank soni 07 Jul, 2014

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"

Om
Om 07 Jul, 2014

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

Rajendra
Rajendra 07 Jul, 2014

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

Nobel
Nobel 11 Jul, 2014

why i am getting this error; could not find function "getUsers"

Pravin
Pravin 17 Jul, 2014

Hello Tavish, I tried it, but getting below error Error: could not find function "getUsers" Please help me. Regards, Pravin.

Siddharth Dubey
Siddharth Dubey 23 Jul, 2014

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.

shalli
shalli 04 Aug, 2014

very good one.

Aurindam dhar
Aurindam dhar 04 Aug, 2014

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??

nitin
nitin 30 Oct, 2014

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?

Olalekan Elesin
Olalekan Elesin 23 Nov, 2014

Thanks Tavish. great post. One problem: the getFriends() function only returns one row. What could be the problem?

Palash
Palash 22 Jan, 2015

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?

sandesh
sandesh 02 Mar, 2015

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

Varun Yadav
Varun Yadav 04 Apr, 2015

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.

Mickael LENOURY
Mickael LENOURY 05 Jun, 2015

I think it's not available now, need to scrape the Facebook page .

Neha Gupta
Neha Gupta 22 Jun, 2015

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?

Lukmanul Hakim
Lukmanul Hakim 11 Oct, 2015

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.

Vishal Gheware
Vishal Gheware 12 Dec, 2016

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.

Shantanu Krishna
Shantanu Krishna 14 Sep, 2017

Hi Tavish, Everyone reading this post. Thanks for this. It seems, since the time you wrote this, many more restrictions have been imposed on what you can access from facebook. But, what i am not able to understand is that why can i not see my details when i connect r with facebook. Specifically, what settings are required to be looked at. Is it the case that only information that you have shared in public is what can be downloaded from fb ( even your own data)?

SANJAY SAINI
SANJAY SAINI 20 Feb, 2018

Hi Tavish, I just started learning R Programming for Facebook data analytics, few days before I am able to manage facebook page post and data download for comments & replies and I am able to do is till 5 Feb'18. post the new update on Facebook API, I and able to download the "Comments" but not the "Replies". Can you please help me to fix it? Thanks For Your support In advance.