Skip to content

Instantly share code, notes, and snippets.

@thoughtfulbloke
Created June 26, 2020 00:56
Show Gist options
  • Save thoughtfulbloke/a435e491c66b0b9a9fc7edc923fad341 to your computer and use it in GitHub Desktop.
Save thoughtfulbloke/a435e491c66b0b9a9fc7edc923fad341 to your computer and use it in GitHub Desktop.
library(readr)
library(dplyr)
library(tidyr)
library(ggplot2)
library(ggthemes)
library(lubridate)
# because we have a lot of csv files of mentions, and only want a few columns
# from each, we will us a custom read function for each csv
custom_read <- function(x){
# where x is the path to the csv file,
# which includes the username as the second last bit (the folder the csv file is in)
username <- rev(unlist(strsplit(x, split="/")))[2]
xcontent <- read_csv(x, col_types =
cols_only(created_at = col_datetime(format = ""),
status_id = col_character(),
user_id = col_character()))
if(nrow(xcontent) > 0){
xcontent$sent_to <- username
return(xcontent)
} else {return(NULL)}
}
# assumes for this script that the working directory has a folder called
# twitter_accounts containing a folders names by twitter screennames,
# contain csv files with download date and the text MN for mentions of
# the account name
readthese <- list.files(path="twitter_accounts",
pattern="MN\\.csv",
full.names=TRUE,
recursive = TRUE)
mntions <- bind_rows(lapply(readthese,custom_read))
weeker <- mntions %>%
select(user_id, created_at, status_id) %>%
distinct() %>% # removing overlap between captures
filter(created_at > ISOdatetime(2020,1,1,0,0,0)) %>% #2020 only
arrange(user_id, created_at) %>%
group_by(user_id) %>% #
slice(1) %>% # gets first contact in 2020
ungroup() %>% #
mutate(week_of = floor_date(created_at, unit="week", week_start = 1),
week_no = as.numeric(factor(week_of))) # for weeks from Mon
ggplot(weeker, aes(x=week_of)) + geom_bar() +
xlab("Week (starting) from Monday") +
theme_tufte() + ggtitle("Total new accounts mentioning
1 or more NZ Plotical Twitter accounts")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment