Scraping web data from Facebook

To scrape data from Facebook’s API, we’ll use the Rfacebook package.

library(Rfacebook)
## Loading required package: httr
## Loading required package: rjson
## Loading required package: httpuv
## 
## Attaching package: 'Rfacebook'
## The following object is masked from 'package:methods':
## 
##     getGroup

To get access to the Facebook API, you need an OAuth code. You can get yours going to the following URL: https://developers.facebook.com/tools/explorer

Once you’re there:
1. Click on “Get Access Token”
2. Copy the long code (“Access Token”) and paste it here below, substituting the fake one I wrote:

fb_oauth = 'EAACEdEose0cBAJR9BNUqLf2CkUjdugM0CssRagoh9hljC6yflhNZC0al4yvmqHPcym5yuTiIp8MiWCPUOxkp7P87tYZAyHNbkp9l7DGLhZA72RICvmRbrZBD2Dxs8yw6ZCZCSFHBfEaldYsxPYeKtFIf7tqL2cqVgoGlX1b1TTV2JET15WXNKzFKJKaYkLoklMgTv4Krt4ZA3bJgv4zNHMH'

Now try running the following line:

getUsers("me", token=fb_oauth, private_info=TRUE)
##          id          name username first_name middle_name last_name gender
## 1 557698085 Pablo Barberá       NA         NA          NA        NA     NA
##   locale likes picture birthday location hometown relationship_status
## 1     NA    NA      NA       NA       NA       NA                  NA

Does it return your Facebook public information? Yes? Then we’re ready to go. See also ?fbOAuth for information on how to get a long-lived OAuth token.

At the moment, the only information that can be scraped from Facebook is the content of public pages.

The following line downloads the ~200 most recent posts on the facebook page of Donald Trump

page <- getPage("DonaldTrump", token=fb_oauth, n=20, reactions=TRUE, api="v2.9") 
## 20 posts

What information is available for each of these posts?

page[1,]
##                               id likes_count      from_id       from_name
## 1 153080620724_10159574464550725       28313 153080620724 Donald J. Trump
##                                                                                                                      message
## 1 Big progress being made in ridding our country of MS-13 gang members and gang members in general. MAKE AMERICA SAFE AGAIN!
##               created_time  type
## 1 2017-07-28T15:50:35+0000 photo
##                                                                                                       link
## 1 https://www.facebook.com/DonaldTrump/photos/a.488852220724.393301.153080620724/10159574463525725/?type=3
##   story comments_count shares_count love_count haha_count wow_count
## 1    NA           3527         1548       3098        489        62
##   sad_count angry_count
## 1        12          56

Which post got more likes, more comments, and more shares?

page[which.max(page$likes_count),]
##                                id likes_count      from_id       from_name
## 11 153080620724_10159589111915725       76269 153080620724 Donald J. Trump
##                                                                                                                                         message
## 11 If ObamaCare is hurting people, & it is, why shouldn't it hurt the insurance companies & why should Congress not be paying what public pays?
##                created_time  type
## 11 2017-07-31T14:16:17+0000 photo
##                                                                                                        link
## 11 https://www.facebook.com/DonaldTrump/photos/a.488852220724.393301.153080620724/10159589111770725/?type=3
##    story comments_count shares_count love_count haha_count wow_count
## 11    NA           9099         9565       8593        819       144
##    sad_count angry_count
## 11        44         188
page[which.max(page$comments_count),]
##                                id likes_count      from_id       from_name
## 18 153080620724_10159594989875725       48259 153080620724 Donald J. Trump
##                                                                                                                                      message
## 18 Only the Fake News Media and Trump enemies want me to stop using Social Media (110 million people). Only way for me to get the truth out!
##                created_time  type
## 18 2017-08-01T17:39:00+0000 photo
##                                                                                                        link
## 18 https://www.facebook.com/DonaldTrump/photos/a.488852220724.393301.153080620724/10159594989190725/?type=3
##    story comments_count shares_count love_count haha_count wow_count
## 18    NA           9228         3029       4666       1721        96
##    sad_count angry_count
## 18        42         122
page[which.max(page$shares_count),]
##                               id likes_count      from_id       from_name
## 8 153080620724_10159584295995725       38623 153080620724 Donald J. Trump
##                                                                      message
## 8 Want to know what President Trump did this week? Watch here for REAL news!
##               created_time  type
## 8 2017-07-30T17:19:00+0000 video
##                                                             link story
## 8 https://www.facebook.com/DonaldTrump/videos/10159584295995725/    NA
##   comments_count shares_count love_count haha_count wow_count sad_count
## 8           6563        14437       5003       1928       153        33
##   angry_count
## 8         212

What about other reactions?

page[which.max(page$love_count),]
##                                id likes_count      from_id       from_name
## 11 153080620724_10159589111915725       76269 153080620724 Donald J. Trump
##                                                                                                                                         message
## 11 If ObamaCare is hurting people, & it is, why shouldn't it hurt the insurance companies & why should Congress not be paying what public pays?
##                created_time  type
## 11 2017-07-31T14:16:17+0000 photo
##                                                                                                        link
## 11 https://www.facebook.com/DonaldTrump/photos/a.488852220724.393301.153080620724/10159589111770725/?type=3
##    story comments_count shares_count love_count haha_count wow_count
## 11    NA           9099         9565       8593        819       144
##    sad_count angry_count
## 11        44         188
page[which.max(page$haha_count),]
##                               id likes_count      from_id       from_name
## 8 153080620724_10159584295995725       38623 153080620724 Donald J. Trump
##                                                                      message
## 8 Want to know what President Trump did this week? Watch here for REAL news!
##               created_time  type
## 8 2017-07-30T17:19:00+0000 video
##                                                             link story
## 8 https://www.facebook.com/DonaldTrump/videos/10159584295995725/    NA
##   comments_count shares_count love_count haha_count wow_count sad_count
## 8           6563        14437       5003       1928       153        33
##   angry_count
## 8         212
page[which.max(page$wow_count),]
##                                id likes_count      from_id       from_name
## 17 153080620724_10159594068540725       36597 153080620724 Donald J. Trump
##                                                                                                                                       message
## 17 Stock Market could hit all-time high (again) 22,000 today. Was 18,000 only 6 months ago on Election Day. Mainstream media seldom mentions!
##                created_time  type
## 17 2017-08-01T13:42:01+0000 photo
##                                                                                                        link
## 17 https://www.facebook.com/DonaldTrump/photos/a.488852220724.393301.153080620724/10159594038085725/?type=3
##    story comments_count shares_count love_count haha_count wow_count
## 17    NA           3946         5834       3612        374       643
##    sad_count angry_count
## 17        14          54
page[which.max(page$sad_count),]
##                               id likes_count      from_id       from_name
## 6 153080620724_10159580158495725       41284 153080620724 Donald J. Trump
##                                                                                                                                                   message
## 6 Republican Senate must get rid of 60 vote NOW! It is killing the R Party, allows 8 Dems to control country. 200 Bills sit in Senate. A JOKE! Get smart!
##               created_time  type
## 6 2017-07-29T18:58:32+0000 video
##                                                             link story
## 6 https://www.facebook.com/DonaldTrump/videos/10159580158495725/    NA
##   comments_count shares_count love_count haha_count wow_count sad_count
## 6           9211         5450       3508       1248       173       131
##   angry_count
## 6         573
page[which.max(page$angry_count),]
##                               id likes_count      from_id       from_name
## 6 153080620724_10159580158495725       41284 153080620724 Donald J. Trump
##                                                                                                                                                   message
## 6 Republican Senate must get rid of 60 vote NOW! It is killing the R Party, allows 8 Dems to control country. 200 Bills sit in Senate. A JOKE! Get smart!
##               created_time  type
## 6 2017-07-29T18:58:32+0000 video
##                                                             link story
## 6 https://www.facebook.com/DonaldTrump/videos/10159580158495725/    NA
##   comments_count shares_count love_count haha_count wow_count sad_count
## 6           9211         5450       3508       1248       173       131
##   angry_count
## 6         573

Let’s do another example, looking at the Facebook page of ECPR:

page <- getPage("ECPRnet", token=fb_oauth, n=100, reactions=TRUE, api="v2.9") 
## 25 posts 50 posts 75 posts 100 posts
# most popular posts
page[which.max(page$likes_count),]
##                                  id likes_count         from_id from_name
## 12 116277705061155_1197688776920037         113 116277705061155      ECPR
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           message
## 12 A joint statement on the situation in Turkey \n\nAs professional associations of social scientists in various disciplines, some of which have members in Turkey, the organizations listed below are deeply concerned about recent reports of infringements of academic freedom in Turkey, in particular in the aftermath of the recent attempt at a coup d’état. \n\nThese reports refer, among other things, to the dismissal of more than 1,500 university deans and professors, and to a ban on foreign travel for all academics in Turkey. \n\nWe do not want to interfere in domestic Turkish politics, but we can and must speak out when academic freedom is at risk. Academic freedom is vital for all disciplines, but particularly so for scholars studying social and political developments. \n\nWe call on the Turkish political authorities to take all possible steps to guarantee that our members, affiliates, and other academics are allowed to continue their scholarly work without interference. \n\nMeanwhile, we offer our sympathy and support to our Turkish members/affiliates and other academics in Turkey.\n\nSigned:\nEuropean Consortium for Political Research (ECPR)\nPolitical Studies Association (UK PSA)
##                created_time   type link story comments_count shares_count
## 12 2016-07-22T14:40:19+0000 status <NA>  <NA>              3           41
##    love_count haha_count wow_count sad_count angry_count
## 12          0          0         0         0           0
page[which.max(page$comments_count),]
##                                  id likes_count         from_id from_name
## 74 116277705061155_1523748194314092          85 116277705061155      ECPR
##                                                                                                                                                                                                                                     message
## 74 Pioneering feminist political scientist Joni Lovenduski has been honoured with this year's ECPR Lifetime Achievement Award - the first woman ever to receive the prize. Congratulations, Joni! \n\nhttps://ecpr.eu/news/news/details/431
##                created_time  type
## 74 2017-05-15T15:03:48+0000 photo
##                                                                                                        link
## 74 https://www.facebook.com/ECPRnet/photos/a.204599966228928.46965.116277705061155/1523748194314092/?type=3
##    story comments_count shares_count love_count haha_count wow_count
## 74  <NA>              6           14         13          0         0
##    sad_count angry_count
## 74         0           0
page[which.max(page$shares_count),]
##                                  id likes_count         from_id from_name
## 12 116277705061155_1197688776920037         113 116277705061155      ECPR
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           message
## 12 A joint statement on the situation in Turkey \n\nAs professional associations of social scientists in various disciplines, some of which have members in Turkey, the organizations listed below are deeply concerned about recent reports of infringements of academic freedom in Turkey, in particular in the aftermath of the recent attempt at a coup d’état. \n\nThese reports refer, among other things, to the dismissal of more than 1,500 university deans and professors, and to a ban on foreign travel for all academics in Turkey. \n\nWe do not want to interfere in domestic Turkish politics, but we can and must speak out when academic freedom is at risk. Academic freedom is vital for all disciplines, but particularly so for scholars studying social and political developments. \n\nWe call on the Turkish political authorities to take all possible steps to guarantee that our members, affiliates, and other academics are allowed to continue their scholarly work without interference. \n\nMeanwhile, we offer our sympathy and support to our Turkish members/affiliates and other academics in Turkey.\n\nSigned:\nEuropean Consortium for Political Research (ECPR)\nPolitical Studies Association (UK PSA)
##                created_time   type link story comments_count shares_count
## 12 2016-07-22T14:40:19+0000 status <NA>  <NA>              3           41
##    love_count haha_count wow_count sad_count angry_count
## 12          0          0         0         0           0
# other reactions
page[which.max(page$love_count),]
##                                  id likes_count         from_id from_name
## 74 116277705061155_1523748194314092          85 116277705061155      ECPR
##                                                                                                                                                                                                                                     message
## 74 Pioneering feminist political scientist Joni Lovenduski has been honoured with this year's ECPR Lifetime Achievement Award - the first woman ever to receive the prize. Congratulations, Joni! \n\nhttps://ecpr.eu/news/news/details/431
##                created_time  type
## 74 2017-05-15T15:03:48+0000 photo
##                                                                                                        link
## 74 https://www.facebook.com/ECPRnet/photos/a.204599966228928.46965.116277705061155/1523748194314092/?type=3
##    story comments_count shares_count love_count haha_count wow_count
## 74  <NA>              6           14         13          0         0
##    sad_count angry_count
## 74         0           0
page[which.max(page$haha_count),]
##                                 id likes_count         from_id from_name
## 1 116277705061155_1136979372990978           3 116277705061155      ECPR
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      message
## 1 Brand new from ECPR Press: Arlo Poletti & Dirk de Bièvre's 'Judicial Politics and International Cooperation\nFrom Disputes to Deal-Making at the World Trade Organization'\n\nhttp://press.ecpr.eu/book_details.asp?bookTitleID=399\n\nJudicial politics has emerged as a central feature of the multilateral trading system, alongside a steep decline in the WTO’s ability to deliver negotiated trade liberalization. \n\nThis book advances innovative arguments and presents original evidence to shed light on the important and under-researched question of whether, and how, judicial politics has affected cooperation in the WTO through multilateral trade rounds. \n\nPraise for this book\n\n'The authors demonstrate persuasively that the strengthening of enforcement in the WTO has had important consequences for the domestic politics of trade in member countries.'\nAndreas Dür, University of Salzburg \n\n'This thought-provoking book is a must-read for all theoreticians and practitioners in the field of international trade.'\nPetros C Mavroidis, Columbia Law School \n\n'Poletti and De Bièvre... offer a novel and nuanced argument by fully integrating domestic interest group dynamics into their theory.'\nManfred Elsig, World Trade Institute, University of Bern
##               created_time  type
## 1 2016-05-18T10:00:35+0000 photo
##                                                                                                       link
## 1 https://www.facebook.com/ECPRnet/photos/a.204599966228928.46965.116277705061155/1136979372990978/?type=3
##   story comments_count shares_count love_count haha_count wow_count
## 1  <NA>              0            0          0          0         0
##   sad_count angry_count
## 1         0           0
page[which.max(page$wow_count),]
##                                  id likes_count         from_id from_name
## 72 116277705061155_1477533715602207         106 116277705061155      ECPR
##                                                                                                                                                                                                                                                              message
## 72 Academic freedom is under threat at Central European University Budapest, host of our summer Methods School. \n\nRead our statement in support of this University's freedom to continue without restriction, and find out how to support the campaign to save it.
##                created_time type                  link story
## 72 2017-03-31T09:44:52+0000 link http://bit.ly/2npR9s3  <NA>
##    comments_count shares_count love_count haha_count wow_count sad_count
## 72              1           41          0          0         1         6
##    angry_count
## 72          12
page[which.max(page$sad_count),]
##                                  id likes_count         from_id from_name
## 72 116277705061155_1477533715602207         106 116277705061155      ECPR
##                                                                                                                                                                                                                                                              message
## 72 Academic freedom is under threat at Central European University Budapest, host of our summer Methods School. \n\nRead our statement in support of this University's freedom to continue without restriction, and find out how to support the campaign to save it.
##                created_time type                  link story
## 72 2017-03-31T09:44:52+0000 link http://bit.ly/2npR9s3  <NA>
##    comments_count shares_count love_count haha_count wow_count sad_count
## 72              1           41          0          0         1         6
##    angry_count
## 72          12
page[which.max(page$angry_count),]
##                                  id likes_count         from_id from_name
## 72 116277705061155_1477533715602207         106 116277705061155      ECPR
##                                                                                                                                                                                                                                                              message
## 72 Academic freedom is under threat at Central European University Budapest, host of our summer Methods School. \n\nRead our statement in support of this University's freedom to continue without restriction, and find out how to support the campaign to save it.
##                created_time type                  link story
## 72 2017-03-31T09:44:52+0000 link http://bit.ly/2npR9s3  <NA>
##    comments_count shares_count love_count haha_count wow_count sad_count
## 72              1           41          0          0         1         6
##    angry_count
## 72          12

We can also subset by date. For example, imagine we want to get all the posts from early November 2012 on Barack Obama’s Facebook page

page <- getPage("barackobama", token=fb_oauth, n=100,
    since='2012/11/01', until='2012/11/10')
## 23 posts
page[which.max(page$likes_count),]
##      from_id    from_name          message             created_time  type
## 5 6815841748 Barack Obama Four more years. 2012-11-07T04:15:08+0000 photo
##                                                                                                   link
## 5 https://www.facebook.com/barackobama/photos/a.53081056748.66806.6815841748/10151255420886749/?type=3
##                             id story likes_count comments_count
## 5 6815841748_10151255420886749  <NA>     4831973         219242
##   shares_count
## 5       660213

And if we need to, we can also extract the specific comments from each post.

post_id <- page$id[which.max(page$likes_count)]
post <- getPost(post_id, token=fb_oauth, n.comments=1000, likes=FALSE)

This is how you can view those comments:

comments <- post$comments
head(comments)
##             from_id       from_name   message             created_time
## 1   509226872540260  Jesse Talafili   OBAMA ! 2012-11-07T04:15:16+0000
## 2   485613484893917 Zain Ahmed Turk      yayy 2012-11-07T04:15:17+0000
## 3      675870897427   Gary D Ploski        <3 2012-11-07T04:15:17+0000
## 4   802034289809838     David Furka       YES 2012-11-07T04:15:18+0000
## 5 10201918108506766      Pinky Keys        :X 2012-11-07T04:15:18+0000
## 6 10102278537299904     Zac Bowling Hell yes! 2012-11-07T04:15:19+0000
##   likes_count comments_count                         id
## 1          18              0 10151255420886749_11954305
## 2           3              0 10151255420886749_11954306
## 3           2              0 10151255420886749_11954307
## 4           5              0 10151255420886749_11954309
## 5           1              0 10151255420886749_11954311
## 6           9              0 10151255420886749_11954315

Also, note that users can like comments! What is the comment that got the most likes?

comments[which.max(comments$likes_count),]
##           from_id      from_name message             created_time
## 1 509226872540260 Jesse Talafili OBAMA ! 2012-11-07T04:15:16+0000
##   likes_count comments_count                         id
## 1          18              0 10151255420886749_11954305

This is how you get nested comments:

page <- getPage("barackobama", token=fb_oauth, n=1)
## 1 posts
post <- getPost(page$id, token=fb_oauth, comments=TRUE, n=100, likes=FALSE)
comment <- getCommentReplies(post$comments$id[1],
                             token=fb_oauth, n=500, likes=TRUE)

If we want to scrape an entire page that contains many posts, given that the API can sometimes give an error, it is a good idea to embed the function within a loop and collect the data by month.

# list of dates to sample
dates <- seq(as.Date("2017/01/01"), as.Date("2017/08/01"), by="1 month")
n <- length(dates)-1
df <- list()
# loop over months
for (i in 1:n){
    message(as.character(dates[i]))
    df[[i]] <- getPage("GameOfThrones", token=fb_oauth, n=1000, since=dates[i],
        until=dates[i+1], verbose=FALSE)
    Sys.sleep(0.5)
}
## 2017-01-01
## 2017-02-01
## 2017-03-01
## 2017-04-01
## 2017-05-01
## 2017-06-01
## 2017-07-01
df <- do.call(rbind, df)
write.csv(df, file="../data/gameofthrones.csv", row.names=FALSE)

And we can then look at the popularity over time:

library(netdemR)
## Loading required package: ROAuth
## Loading required package: jsonlite
## 
## Attaching package: 'jsonlite'
## The following objects are masked from 'package:rjson':
## 
##     fromJSON, toJSON
## ##
## ## netdemR: tools for analysis of Twitter data
## ## Networked Democracy Lab at USC
## ## netdem.org
## ##
## 
## Attaching package: 'netdemR'
## The following objects are masked from 'package:Rfacebook':
## 
##     getFriends, getUsers
library(stringr)
library(reshape2)
# parse date into month
df$month <- df$created_time %>% str_sub(1, 7) %>% paste0("-01") %>% as.Date()
# computing average by month
metrics <- aggregate(cbind(likes_count, comments_count, shares_count) ~ month,
          data=df, FUN=mean)
# reshaping into long format
metrics <- melt(metrics, id.vars="month")
# visualize evolution in metric
library(ggplot2)
library(scales)
ggplot(metrics, aes(x = month, y = value, group = variable)) + 
  geom_line(aes(color = variable)) + 
    scale_x_date(date_breaks = "years", labels = date_format("%Y")) + 
  scale_y_log10("Average count per post", 
    breaks = c(10, 100, 1000, 10000, 100000, 200000), labels=scales::comma) + 
  theme_bw() + theme(axis.title.x = element_blank())

Just like public Facebook pages, the data from public groups can also be easily downloaded with the getGroup function. Note that this will only work for groups that the authenticated user is a member of.

group <- getGroup("150048245063649", token=fb_oauth, n=50)
## 25 posts 50 posts

If you own or administer a public page, you will be able to get some statistics about the users of that page using getInsights. The only page statistic that is publicly available is the number of users per country:

insights <- getInsights('DonaldTrump', 
     token=fb_oauth, metric='page_fans_country', period='lifetime')
# selecting data as of today
insights <- insights[grep("2017-07-31", insights$end_time),]
# countries with most fans
head(insights[order(insights$value, decreasing=TRUE),])
##                                                  id              name
## 87 153080620724/insights/page_fans_country/lifetime page_fans_country
## 64 153080620724/insights/page_fans_country/lifetime page_fans_country
## 51 153080620724/insights/page_fans_country/lifetime page_fans_country
## 74 153080620724/insights/page_fans_country/lifetime page_fans_country
## 72 153080620724/insights/page_fans_country/lifetime page_fans_country
## 50 153080620724/insights/page_fans_country/lifetime page_fans_country
##      period                     title
## 87 lifetime Lifetime Likes by Country
## 64 lifetime Lifetime Likes by Country
## 51 lifetime Lifetime Likes by Country
## 74 lifetime Lifetime Likes by Country
## 72 lifetime Lifetime Likes by Country
## 50 lifetime Lifetime Likes by Country
##                                                                                                            description
## 87 Lifetime: Aggregated Facebook location data, sorted by country, about the people who like your Page. (Unique Users)
## 64 Lifetime: Aggregated Facebook location data, sorted by country, about the people who like your Page. (Unique Users)
## 51 Lifetime: Aggregated Facebook location data, sorted by country, about the people who like your Page. (Unique Users)
## 74 Lifetime: Aggregated Facebook location data, sorted by country, about the people who like your Page. (Unique Users)
## 72 Lifetime: Aggregated Facebook location data, sorted by country, about the people who like your Page. (Unique Users)
## 50 Lifetime: Aggregated Facebook location data, sorted by country, about the people who like your Page. (Unique Users)
##       value                 end_time variable
## 87 11680122 2017-07-31T07:00:00+0000       US
## 64  1026884 2017-07-31T07:00:00+0000       IN
## 51   477866 2017-07-31T07:00:00+0000       BR
## 74   437265 2017-07-31T07:00:00+0000       NG
## 72   373657 2017-07-31T07:00:00+0000       MX
## 50   361084 2017-07-31T07:00:00+0000       BD
# fans outside the US
sum(insights$value[insights$variable!="US"]) /
  sum(insights$value)
## [1] 0.4223839
insights <- getInsights('HillaryClinton', 
     token=fb_oauth, metric='page_fans_country', period='lifetime')
# selecting data as of today
insights <- insights[grep("2017-07-31", insights$end_time),]
# countries with most fans
head(insights[order(insights$value, decreasing=TRUE),])
##                                                     id              name
## 87 889307941125736/insights/page_fans_country/lifetime page_fans_country
## 72 889307941125736/insights/page_fans_country/lifetime page_fans_country
## 65 889307941125736/insights/page_fans_country/lifetime page_fans_country
## 50 889307941125736/insights/page_fans_country/lifetime page_fans_country
## 88 889307941125736/insights/page_fans_country/lifetime page_fans_country
## 74 889307941125736/insights/page_fans_country/lifetime page_fans_country
##      period                     title
## 87 lifetime Lifetime Likes by Country
## 72 lifetime Lifetime Likes by Country
## 65 lifetime Lifetime Likes by Country
## 50 lifetime Lifetime Likes by Country
## 88 lifetime Lifetime Likes by Country
## 74 lifetime Lifetime Likes by Country
##                                                                                                            description
## 87 Lifetime: Aggregated Facebook location data, sorted by country, about the people who like your Page. (Unique Users)
## 72 Lifetime: Aggregated Facebook location data, sorted by country, about the people who like your Page. (Unique Users)
## 65 Lifetime: Aggregated Facebook location data, sorted by country, about the people who like your Page. (Unique Users)
## 50 Lifetime: Aggregated Facebook location data, sorted by country, about the people who like your Page. (Unique Users)
## 88 Lifetime: Aggregated Facebook location data, sorted by country, about the people who like your Page. (Unique Users)
## 74 Lifetime: Aggregated Facebook location data, sorted by country, about the people who like your Page. (Unique Users)
##      value                 end_time variable
## 87 4349008 2017-07-31T07:00:00+0000       US
## 72  443686 2017-07-31T07:00:00+0000       MX
## 65  407760 2017-07-31T07:00:00+0000       IN
## 50  387021 2017-07-31T07:00:00+0000       BD
## 88  225822 2017-07-31T07:00:00+0000       VN
## 74  209182 2017-07-31T07:00:00+0000       NG
# fans outside the US
sum(insights$value[insights$variable!="US"]) /
  sum(insights$value)
## [1] 0.506369