Skip to content

Instantly share code, notes, and snippets.


Matt Dray matt-dray

View GitHub Profile
matt-dray / multi_plot_loop.R
Created Jan 15, 2017
Plot each y column against x and arrange in grids over multiple pages
View multi_plot_loop.R
library(ggplot2) # for plotting
library(gridExtra) # for arranging plots
# Create example dataframe
df <- data.frame(x = letters[1:10]
, y1 = sample(1:100, 10)
, y2 = sample(1:100, 10)
, y3 = sample(1:100, 10)
, y4 = sample(1:100, 10)
, y5 = sample(1:100, 10)
matt-dray / loop_specify_combinations.R
Last active Sep 5, 2017
Loop through plots of specified column pairs of a dataframe
View loop_specify_combinations.R
# purpose: loop through plots of specified column pairs of a dataframe
# don't want to compare all possible permutations
# also avoids nested loop of x and y
library(tidyverse) # for tibble and ggplot
# generate example dataframe
test_data <- tibble::tibble(x1 = rnorm(100)
, x2 = rnorm(100)
matt-dray / loop_multi_file_assign_name.R
Created Sep 5, 2017
Loop read multiple files and assign object name based on filename
View loop_multi_file_assign_name.R
# list files in directory (in the format "x_y_z.csv")
list_files <- list.files("file/path")
for (i in seq_along(1:length(list_files))) {
# read the data
matt-dray / googlesheets_test.R
Last active Sep 19, 2018
Basic functions from the googlesheets package for R
View googlesheets_test.R
# Basic googlesheets functions
# Jan 2018
# Browser sign-in required on first function execution
# 1. Load package ----
matt-dray / preferred_rmd_yaml.yaml
Last active Sep 19, 2018
R Markdown YAML header for current date, nice theme/highlighting/table of contents
View preferred_rmd_yaml.yaml
title: "Title"
subtitle: "Subtitle"
author: "Name"
date: "`r format(Sys.time(), '%d %B, %Y')`"
theme: cerulean
highlight: tango
number_sections: yes

Keybase proof

I hereby claim:

  • I am matt-dray on github.
  • I am mattdray ( on keybase.
  • I have a public key ASD-cOiAVRNCc2XIcTt3U-nwNwA32uzNhPZPmHDiBDH8pAo

To claim this, I am signing this object:

matt-dray / extract_tweets_function.R
Last active Sep 19, 2018
Function to automate generation of RDS, simple CSV and plot using rtweet
View extract_tweets_function.R
# Function to automate generation of RDS, simple CSV and plot using rtweet
# Matt Dray
# March 2018
# Purpose: create an RDS, simplified CSV and plot of tweets containing search term
# from rtweet::get_tweets function and save to to folder with unique descriptive
# name related to search term. Assumes you have an 'output' folder in your home
# directory to store these files. Assumes you've already sorted out a twitter
# token as per
matt-dray / bin-using-hist-breaks.R
Created May 9, 2018
Using the binwidths of a histogram object to bin values in the dataframe
View bin-using-hist-breaks.R
# 1. Fake dataset
df <- data.frame(id = 1:1000, value = sample(10000:50000, 1000))
# 2. Histogram object for accessing binwidths
hist_df <- hist(
df$value, # column of data to be binned
(50000-10000)/500 # bins of width 500 from 10k to 50k
matt-dray / dfe_colour_palette.R
Created May 17, 2018
Convert RGB to hex for dept colour palette
View dfe_colour_palette.R
# Approved colour palette for DfE
# Convert to hex the RGB values provided by the dept
# Matt Dray
# May 2018
# the main need is for plotting stats in publications
# inspiration from
# this may eventually be added to the dfeR package
# function for converting from RGB to hex
matt-dray / Rcrawler-link-extract.R
Created May 22, 2018
Using the Rcrawler package to extract hyperlinks containing certain strings from a website
View Rcrawler-link-extract.R