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.
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”.
The easiest way is to select all permissions.
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.
Click on “Okay” and you can find your Access Token in the box.
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.
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.
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:
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.
If you have any questions feel free to ask me on Twitter or write a comment here.