Skip to content

Instantly share code, notes, and snippets.

View matt-dray's full-sized avatar
®️

Matt Dray matt-dray

®️
View GitHub Profile
@matt-dray
matt-dray / multi_plot_loop.R
Created January 15, 2017 14:02
Plot each y column against x and arrange in grids over multiple pages
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
matt-dray / loop_specify_combinations.R
Last active September 5, 2017 14:17
Loop through plots of specified column pairs of a dataframe
# 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
matt-dray / loop_multi_file_assign_name.R
Created September 5, 2017 14:16
Loop read multiple files and assign object name based on filename
library(tidyverse)
library(stringr)
# 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
matt-dray / googlesheets_test.R
Last active September 19, 2018 13:27
Basic functions from the googlesheets package for R
# Basic googlesheets functions
# Jan 2018
# https://github.com/jennybc/googlesheets
# Browser sign-in required on first function execution
# 1. Load package ----
#install.packages(googlesheets)
library(googlesheets)
@matt-dray
matt-dray / preferred_rmd_yaml.yaml
Last active September 19, 2018 13:26
R Markdown YAML header for current date, nice theme/highlighting/table of contents
---
title: "Title"
subtitle: "Subtitle"
author: "Name"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
html_document:
theme: cerulean
highlight: tango
number_sections: yes

Keybase proof

I hereby claim:

  • I am matt-dray on github.
  • I am mattdray (https://keybase.io/mattdray) on keybase.
  • I have a public key ASD-cOiAVRNCc2XIcTt3U-nwNwA32uzNhPZPmHDiBDH8pAo

To claim this, I am signing this object:

@matt-dray
matt-dray / extract_tweets_function.R
Last active September 19, 2018 13:25
Function to automate generation of RDS, simple CSV and plot using rtweet
# 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 http://rtweet.info/articles/auth.html
@matt-dray
matt-dray / bin-using-hist-breaks.R
Created May 9, 2018 09:24
Using the binwidths of a histogram object to bin values in the dataframe
# 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
matt-dray / dfe_colour_palette.R
Created May 17, 2018 13:19
Convert RGB to hex for dept colour palette
# 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 https://github.com/ukgovdatascience/govstyle
# this may eventually be added to the dfeR package
# function for converting from RGB to hex
@matt-dray
matt-dray / Rcrawler-link-extract.R
Created May 22, 2018 10:55
Using the Rcrawler package to extract hyperlinks containing certain strings from a website