Analyzing Facebook Networks using #rstats

Analyzing Facebook Networks using #rstats is given below. Note this can be replicated by anyone- and you can theoretically be able to create adjacency matrix (friend of friends) of all people who have given permission into your app- thus maybe a network analysis of people to some group (like R Facebook group or even FOASStat is possible

While traditional marketing and targeting focusses on variables like demographics and psychographics, social network analysis lets you tap into influential people within communities with the right offer and then let the free retweet, reshare as proxy free ads. The hypothesis being that people in similar network clusters have similar consumer behaviours (not just in similar demographic or psychographics). Also this should ideally be able to capture changing consumer behaviour faster for better targeting.


1) Go to

2) Create a new app

Screenshot 2014-05-10 21.30.53

3) Change url in settings ( to http://localhost:1410 ). I wish we could do this in twitteR package and oAuth for TWitter but I have no clue . that. Also coming up post on Google Plus and LinkedIn (if I can)

Screenshot 2014-05-10 21.32.01

3) Copy the secret thingies from the dashboard. DO click the screenshots to enlarge (i.e to understand what I am trying to do here)

Screenshot 2014-05-10 21.25.00

4) Modify code below with the secret thingies above

#fbOAuth(app_id, app_secret, extended_permissions = TRUE)
fb_oauth=fbOAuth("241634542707615", "4f2e6273c08d1b56d1bf14626d9e19a6", extended_permissions = TRUE)
#fb_oauth <- fbOAuth(app_id="123456789", app_secret="1A2B3C4D")
save(fb_oauth, file="fb_oauth")

Created by Pretty R at

5) Hey now we have a fb ouath object we can just load it up. we the following code can then be modified at will to get your analysis

me <- getUsers("me", token=fb_oauth)
#getFQL(query, token)
my_friends <- getFriends(token=fb_oauth, simplify=F)
pie(table(my_friends$relationship_status),col=brewer.pal(5, "Set1"))
pie(table(my_friends$location),col=brewer.pal(20, "Greens"))
pie(table(my_friends$locale),col=brewer.pal(4, "Blues"))
pie(table(my_friends$gender),col=brewer.pal(3, "Oranges"))
mat <- getNetwork(token=fb_oauth, format="adj.matrix")
network <- graph.adjacency(mat, mode="undirected")
plot(network ,vertex.size=5, 

Created by Pretty R at

6) Voila my FB graph


and analyzed for how are my homies


Europeans into open relationships more


Places my friends are


Languages they speak

and all with R

Note- this is what an adjancency matrix is- just a fancy word for turning your Fb friends into a 1-0 matrix based on their relationships. The more friends you have the more interesting this analysis

Now lets do likes analysis

I use this code and my own username (since I am the only one who has allowed the app permissions!)


Boy I like a lot of websites huh- I wonder if I can cluster/ data reduce/association analysis  them (but that’s coming up  or is it?)


Screenshot 2014-05-23 01.31.42


Screenshot 2014-05-10 21.48.56

Hat tips-


Author: Ajay Ohri

9 thoughts on “Analyzing Facebook Networks using #rstats”

  1. Hi there,
    I am getting an error while using getFriends function. The error is as following :

    Only friends who use the application will be returned
    See ?getFriends for more details
    Error in getFriends(token = fb_oauth, simplify = F) :
    No friend information is available.

    could you please help to resolve this?

  2. I have a same ploblem like Mohammad Daoud.

    Only friends who use the application will be returned
    See ?getFriends for more details
    Error in getFriends(token = FB_OAUTH, simplify = F) :
    No friend information is available.

    how can i solve this error?
    does anyone have idea?

  3. my_friends <- getFriends(token=fb_oauth, simplify=F)
    Only friends who use the application will be returned
    See ?getFriends for more details
    Error in getFriends(token = fb_oauth, simplify = F) :
    No friend information is available.

  4. Just a perfect way to spend a lazy Sunday evening although GOT is coming up on HBO in an hour!! great post Ajay!

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s

%d bloggers like this: