Last active
March 16, 2022 06:48
-
-
Save erictleung/ca360b6375b129aa6499e3470e1d8651 to your computer and use it in GitHub Desktop.
Identify accounts on Twitter that you follow that do not tweet frequently or are just inactive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Script to identify Twitter accounts that have not tweeted in a while or just | |
# infrequent. | |
# Modified from: | |
# https://twitter.com/tomstafford/status/1160553156633174017 | |
# Load libraries | |
library(rtweet) | |
library(dplyr) | |
# Set variables | |
your_username <- "erictleung" | |
month_threshold <- 12 | |
# Which user are we looking at who they follow | |
fds <- get_friends(your_username) | |
# What is the threshold which makes an account defunct? | |
day_threshold <- month_threshold * 30 # About 30 days/month | |
# Get data on the people you follow | |
user_followers <- fds %>% | |
pull(user_id) %>% | |
lookup_users() | |
# Create datatime object for today | |
today <- Sys.time() %>% | |
format("%Y-%m-%d %H:%M:%S") | |
as.POSIXct(tz = Sys.timezone()) | |
# Find people I follow who haven't tweeted in a while | |
defunct <- | |
user_followers %>% | |
# Keep only necessary information | |
select(screen_name, created_at) %>% | |
# Calculate number of days from today since user last tweeted | |
mutate(days_since = difftime(today, | |
as.POSIXct(created_at, tz = Sys.timezone()), | |
units = "days")) %>% | |
filter(days_since > day_threshold) %>% | |
# Instead of days, let's see weeks | |
mutate(weeks_since = difftime(today, | |
as.POSIXct(created_at, tz = Sys.timezone()), | |
units = "weeks")) %>% | |
# Just keep list of names | |
select(screen_name) | |
# See results | |
View(defunct) | |
# Write out results | |
file_out <- paste(format(Sys.time(), "%Y-%m-%d"), "defunct_accts.txt", sep = "_") | |
write.table(defunct, file_out, sep = "\t") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment