Analyze Facebook with R

Hello everybody! Today I found something very cool: There is a R package for mining Facebook. For Twitter there are different, but this is the first one really working well with Facebook. So I wanted to test it and was amazed about how easy it works.

Setup:

First we have to get an access_token for our connection. To get it go to https://developers.facebook.com/tools/explorer

As API version use “Unversioned” and then click on “Get Access Token”.access token facebook open graph r

The easiest way is to select all permissions.

access token dialog R facebook

Then click on “Get Access Token” again and a dialog will appear which asks you to agree that the token can get all the information you just selected.

facebook dialog R

Click on “Okay” and you can find your Access Token in the box.

 

R

First we need to install the packages Rfacebook and Rook It is important to write it exactly this way as there is an older package with nearly the same name, but it won´t work. cd4ef1dbf77a43117054ba8865c4ace7 1185f3caaf97d7b975caf386c31c9582

Ok now we need to connect our R session with our test app and authenticate it to our Facebook Profile for Data Mining. Rfacebook offers a very easy function for that. Note: We are using a temporary access token in this example. You can also you authentication with an app, but there seems to be a problem with the Rfacebook package. I will add it again when it is working again.

Get Data!

Now we connected everything and have access to Facebook. We will start with getting our own profile information.

The getUsers function return public information about one or more Facebook user. If we use “me” as the username argument, it will return our own profile info.


me <- getUsers("me",token=token)

Now we saved our own public information in the variable „me“

For the case you forgot your name ;)

Let´s take a look at our friends.
Rfacebook offers the function


getFriends(token, simplify = FALSE)

so you get your friends with:

my_friends <- getFriends(token, simplify = FALSE)

Your friends list is ordered by ID, so you can see who of your friends was the first on Facebook. Some very interesting inside.

You can show the friends which are the longest time on Facebook with:

head(my_friends, n=10)

But let´s go a step further and get some deeper information about our friends. We now have the variable my_friends. We can combine it with the getUsers() function.

 my_friends_info <- getUsers(my_friends$id, token=token, private_info=TRUE)

For example to get the relationship status of our friends.


table(my_friends_info$relationship_status)

 

d13beec3b80973ce409a8ebbb268d0c7

Questions?

If you have any questions feel free to ask me on Twitter or write a comment here.

About these ads

114 thoughts on “Analyze Facebook with R

  1. after getting access token i run the following commands
    token<-"xxxxxxx"
    me<-getUsers("me", token)
    my_friends <- getFriends(token, simplify = FALSE).
    but no output generated

    • Ok what is stored in the variable “me” after you run me<-getUsers("me", token) ?

      Did you check all boxes in the "Select Permission" box before you create the access token?

      Regards

    • No this is not right. In the variable me there should be your own profile information.
      What is the output from:
      getUsers(“me”, token)
      Just paste this line in the console and show me the output please.

    • Now you can go on like I described in my tutorial with my_friends <- getFriends(token, simplify = FALSE)
      and you should get your list of friends.

      Regards

    • You have to store the information. Please just follow the steps in my tutorial.
      Save it with
      me <- getUsers("me",token=token)
      And get friends with:
      my_friends <- getFriends(token, simplify = FALSE)

  2. getUsers(“me”, token)
    me <- getUsers("me",token=token)
    my_friends <- getFriends(token, simplify = FALSE)
    these were the commands which i used which must give my friend list but output wasn't generated.

  3. [1] id name username first_name
    [5] last_name gender locale category
    [9] likes picture birthday location
    [13] hometown relationship_status
    (or 0-length row.names)

    this is the output.

    • So there is everything right with the R code. It just says that you don´t have friends on Facebook. So the Facebook API did not return any friends.
      You also checked all boxes on the second tab of the “Select Permissions” dialog? Check all boxes in the tab “Friends Data Permissions”.

    • So you are logged in with your real Facebook account to get the token?
      Do you have Facebook friends on this account?
      The Facebook API does say that there are no friends on the account you created the Access Token with.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s