Authentication:
Gender Analysis of Facebook Post Likes
1 2 |
install.packages(„Rfacebook“) require(Rfacebook) |
1 |
token <- „XXX" |
1 |
number_posts <- 2 |
1 |
page_name <- „forbes" |
1 |
page <- getPage(page_name, token, n = number_posts, feed = FALSE) |
1 |
posts <- page$id |
Get Post Like details
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
data_frame_gender <- data.frame(post=character(),male=numeric(),female=numeric(),etc=numeric(),likes=numeric(),type=character(),stringsAsFactors=FALSE) for(i in 1:length(posts)) { temp <- posts[i] #dataframe values: #post id #likes count # post <- getPost(temp,token) data_frame_gender[i,1] <- post$post$message data_frame_gender[i,5] <- post$post$likes data_frame_gender[i,6] <- post$post$type gender_frame <- data.frame(gender=character(),stringsAsFactors=FALSE) for(j in 1:length(post$likes$from_id)) { likes <- post$likes$from_id user_id <- likes[j] user <- getUsers(user_id,token=token) gender <- user$gender gender_frame[nrow(gender_frame)+1,] <- gender } number_males <- nrow(subset(gender_frame, gender=="male")) number_females <- nrow(subset(gender_frame, gender=="female")) number_etc <- data_frame_gender[i,5] - (number_males+number_females) data_frame_gender[i,2] <- number_males data_frame_gender[i,3] <- number_females data_frame_gender[i,4] <- number_etc } |
Plot the data
1 2 3 4 5 6 7 8 |
slices <- c(sum(data_frame_gender$male),sum(data_frame_gender$female),sum(data_frame_gender$etc)) pct <- round(slices/sum(slices)*100) lbls <- names(data_frame_gender[2:4]) lbls <- paste(lbls, pct) # add percents to labels lbls <- paste(lbls,"%",sep="") # ad % to labels pie(slices, labels = lbls, main="Gender Distribution of all analyzed posts") |
https://github.com/JulianHill/R-Tutorials/blob/master/r_facebook_gender.r