Skip to content

Instantly share code, notes, and snippets.

@paldhous
Last active September 30, 2019 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paldhous/4eeb5c7c56828285fda7d5a10e03ef29 to your computer and use it in GitHub Desktop.
Save paldhous/4eeb5c7c56828285fda7d5a10e03ef29 to your computer and use it in GitHub Desktop.
# load required packages
library(rtweet)
library(tidyverse)
# get tweets (you will need a Twitter API token to run this code)
edge <- get_timeline("edge", n = 3200)
# function for frequency table
count_pct <- function(df) {
return(
df %>%
tally() %>%
mutate(n_pct = 100*n/sum(n))
)
}
# gender of tweets about Edge final question
questions <- edge %>%
filter(grepl("asks his final|asks her final|asks their final|ask their final", text, ignore.case = TRUE)) %>%
mutate(gender = case_when(grepl("asks his", text) == TRUE ~ "man",
grepl("asks her", text) == TRUE ~ "woman",
grepl("asks their", text) == TRUE ~ "neutral singular",
grepl("ask their", text) == TRUE ~ "neutral plural"))
questions_gender <- questions %>%
group_by(gender) %>%
count_pct()
# top mentioned accounts
mentions <- edge %>%
filter(!is.na(mentions_screen_name)) %>%
select(mentions_screen_name) %>%
unnest() %>%
filter(mentions_screen_name != "edge") %>%
group_by(mentions_screen_name) %>%
count_pct() %>%
arrange(-n)
# top retweeted accounts
retweets <- edge %>%
filter(!is.na(retweet_screen_name)) %>%
select(retweet_screen_name) %>%
filter(retweet_screen_name != "edge") %>%
group_by(retweet_screen_name) %>%
count_pct() %>%
arrange(-n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment