Skip to content

Instantly share code, notes, and snippets.

@thoughtfulbloke
thoughtfulbloke / DSTcheck.R
Last active October 28, 2020 21:37
Checks twitter accounts daylight savings movements.
# modify this line
questionable_screennames <- c("put","screennames","here")
###
library(rtweet)
library(dplyr)
library(lubridate)
library(ggplot2)
library(scales)
library(ggthemes)
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){
@thoughtfulbloke
thoughtfulbloke / impute_apple_mobility_missing_days.R
Last active August 28, 2020 20:14
Imputes the 11th and 12th of May in Apple Mobility Data
library(lubridate)
library(tidyr)
library(dplyr)
apple <- read.csv("~/Desktop/applemobilitytrends-2020-08-26.csv",
stringsAsFactors = FALSE)
# columns X2020.05.11 X2020.05.12 are missing, so for that Mon and Tue
# working out the median relationships between Sun and Wed and Mon and Tue
# and applying them using the Sun and Wed of that week.
library(dplyr)
library(tidyr)
library(lubridate)
library(ggplot2)
library(countrycode)
world <- readr::read_csv("https://population.un.org/wpp/Download/Files/1_Indicators%20(Standard)/CSV_FILES/WPP2019_TotalPopulationBySex.csv")
pops <- world %>% filter(Variant == "Medium") %>%
mutate(country_code = countrycode(Location, origin = 'country.name', destination = 'iso3c')) %>%
select(year=Time, country_code, PopTotal)
mort <- readr::read_csv("https://www.mortality.org/Public/STMF/Outputs/stmf.csv", skip = 1) %>%
library(dplyr)
library(countrycode)
library(lubridate)
library(tidyr)
library(ggplot2)
library(ggthemes)
library(ggrepel)
# apple mobility data csv from
# https://www.apple.com/covid19/mobility
amt <- read.csv("~/Desktop/applemobilitytrends-2020-05-20.csv", colClasses = "character",
library(dplyr)
library(lubridate)
library(ggplot2)
library(ggthemes)
# European Centre for Disease Control and Prevention
EUcdc <- read.csv("https://opendata.ecdc.europa.eu/covid19/casedistribution/csv",
stringsAsFactors = FALSE, fileEncoding = "UTF-8-BOM")
highlight_countries <- c("New_Zealand", "United_Kingdom", "United_States_of_America",
"Taiwan", "Sweden", "Vietnam", "Australia")
@thoughtfulbloke
thoughtfulbloke / NZvsUK.R
Created May 6, 2020 10:39
code for NZ vs UK to show similarity in early progression
library(dplyr)
library(lubridate)
library(ggplot2)
EUcdc <- read.csv("https://opendata.ecdc.europa.eu/covid19/casedistribution/csv",
stringsAsFactors = FALSE, fileEncoding = "UTF-8-BOM")
# NZ level 4 lockdown 26 March
#Boris Handshakes 3 March,
EUcdc %>%
filter(countriesAndTerritories %in% c("United_Kingdom", "New_Zealand")) %>%
@thoughtfulbloke
thoughtfulbloke / Apple_Mobility_example.R
Created May 4, 2020 23:51
Apple Mobility Data example
library(readr)
library(dplyr)
library(tidyr)
library(lubridate)
library(ggplot2)
# from the downloaded apple mobiblity data of the day
mobl <- read_csv("applemobilitytrends-2020-05-03.csv")
# colums added for each day, so get number of coluns in current data
extant <- ncol(mobl)
# graph specific choices
library(dplyr)
library(ggplot2)
library(scales)
library(patchwork)
# random data, ordered
set.seed(2020)
series1_ind <- sort(sample(0:100, 25, replace=TRUE))
series2_ind <- sort(sample(1000:30000, 25, replace=TRUE))
example <- data_frame(step=1:25, series1_ind, series2_ind)
# https://www.mbie.govt.nz/immigration-and-tourism/tourism-research-and-data/tourism-data-releases/monthly-regional-tourism-estimates/regional-tourism-estimates/regional-tourism-estimates-key-pivot-table/
# Regional Tourism Estimates key pivot table
library(readxl)
library(tidyr)
library(dplyr)
library(ggplot2)
library(forcats)
tourism <- read_excel("rte-pivot-table-ye-march-2015.xlsx", sheet="Database")
TA_tour <- tourism %>% filter(YEMar == 2015) %>%