Learn everything about Analytics

Using Facebook as an analyst (Hint – using R)

SHARE
, / 46

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.

 

46 Comments

  • Gaurav says:

    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 says:

    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 says:

    very interesting

  • Tavish says:

    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 says:

    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

    • Tavish says:

      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

  • Vani P says:

    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 says:

    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 says:

    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?

    • Tavish says:

      Ravi,

      This looks like a permission issue. You can check this out using any of your friends FB token.
      Tavish

  • Vikesh says:

    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 says:

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

  • Ajay says:

    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 says:

    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 says:

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

    Thanks

  • Dhanasekaran says:

    Hi Tavish,

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

  • mayank soni says:

    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 says:

    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 says:

    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

    • Tavish says:

      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.

  • Nobel says:

    why i am getting this error;

    could not find function “getUsers”

  • Pravin says:

    Hello Tavish,

    I tried it, but getting below error

    Error: could not find function “getUsers”

    Please help me.

    Regards,
    Pravin.

    • Tavish Srivastava says:

      Looks like your library is not loaded.

    • rathankar says:

      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

  • Siddharth Dubey says:

    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.

    • Tavish Srivastava says:

      Try the code with some other’s facebook token. If it works probably its an issue with your FB settings

  • shalli says:

    very good one.

  • Aurindam dhar says:

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

    • Tavish says:

      Aurindham,
      You can try placing this package directly to your R library folder after downloading it from CRAN website archive folder.

      Tavish

  • nitin says:

    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 says:

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

  • Palash says:

    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 says:

    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 says:

    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 says:

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

  • Neha Gupta says:

    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?

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

  • Lukmanul Hakim says:

    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.

Leave A Reply

Your email address will not be published.

Join world’s fastest growing Analytics Community
Receive awesome tips, guides, infographics and become expert at: