Skip to content

Instantly share code, notes, and snippets.

View johnmackintosh's full-sized avatar

John MacKintosh johnmackintosh

View GitHub Profile
library(rgdal)
library(dplyr)
library(readr)
library(stringi)
library(stringr)
library(tidyr)
library(grid)
library(scales)
library(ggplot2)
library(ggthemes)
@pteetor
pteetor / toyApp.R
Created November 20, 2015 14:01
Splitting an R application into modular parts
#
# This is a toy example of splitting an R application
# into three distinct parts:
#
# - Loading the data
# - Calculating the analytics
# - Rendering an RMarkdown document
#
# Note: The RMarkdown doc appears in a companion file, toyDoc.Rmd.
#
@fredbenenson
fredbenenson / kickstarter_sql_style_guide.md
Last active May 22, 2025 23:58
Kickstarter SQL Style Guide
layout title description tags
default
SQL Style Guide
A guide to writing clean, clear, and consistent SQL.
data
process

Purpose

@stefanwalther
stefanwalther / loadscript.vbs
Created March 15, 2016 20:19
Dynamic Qlik Load Script
// Change the amount here to create more records
SET vAmountTransactions=10000;
Characters:
Load Chr(RecNo()+Ord('A')-1) as Alpha, RecNo() as Num autogenerate 26;
ASCII:
Load
if(RecNo()>=65 and RecNo()<=90,RecNo()-64) as Num,
Chr(RecNo()) as AsciiAlpha,
@kdkorthauer
kdkorthauer / RstudioServerSetup.sh
Created October 7, 2016 15:04
Bash script to set up R, install a few R packages, and get Rstudio Server running on ubuntu.
sudo sh -c 'echo "deb http://cran.rstudio.com/bin/linux/ubuntu trusty/" >> /etc/apt/sources.list'
gpg --keyserver keyserver.ubuntu.com --recv-key E084DAB9
gpg -a --export E084DAB9 | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install r-base libapparmor1 libcurl4-gnutls-dev libxml2-dev libssl-dev gdebi-core
sudo apt-get install libcairo2-dev
sudo apt-get install libxt-dev
sudo apt-get install git-core
sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
@stephlocke
stephlocke / mora.R
Last active April 6, 2024 01:37
Additive or multiplicative time series?
if(!require("ggseas")) install.packages("ggseas")
if(!require("forecast")) install.packages("forecast")
if(!require("data.table")) install.packages("data.table")
if(!require("knitr")) install.packages("knitr")
library(ggseas)
library(forecast)
library(data.table)
# Get data
@tgh0831
tgh0831 / foverlaps example.r
Created November 1, 2016 20:58
Fast Overlaps example using the data.table package's foverlaps() function. Uses lubridate to convert text values for date and time to POSIXct
require(data.table)
require(lubridate)
#create the data frames
#x is a data table with a list of events to match against the different dates in x to see if there is overlap
x = data.table(
eventid=c(1,2,3),
start =mdy_hms(c('10/1/2016 04:30:00','10/1/2016 18:02:00','10/2/2016 14:21:00')),
end =mdy_hms(c('10/1/2016 05:43:00','10/2/2016 01:23:00','10/4/2016 08:54:00'))
@jgilfillan
jgilfillan / cumsum_with_reset.R
Created February 3, 2017 12:44
Cumulative sum in R with reset after reaching threshold
testvector <- c(.5, .1, .2, .9, .9, .2, .5)
# group rows based on cumsum with reset
cumsum_group <- function(x, threshold) {
cumsum <- 0
group <- 1
result <- numeric()
for (i in 1:length(x)) {
cumsum <- cumsum + x[i]
@nacnudus
nacnudus / data.table-joins.R
Created August 21, 2017 09:20
How to do joins with data.table
library(data.table)
?`[.data.table`
DT <- data.table(x=rep(c("b","a","c"),each=3), y=c(1,3,6), v=1:9)
X <- data.table(x=c("c","b"), v=8:7, foo=c(4,2))
colnames(DT)
# [1] "x" "y" "v"
@JosiahParry
JosiahParry / multi_album_artists.txt
Created January 29, 2018 14:57
Multiple Album Tracklists and Lyrics
# Create a tibble with the artist's name & another with the album title
kdot_albums <- tibble(
artist = "Kendrick Lamar",
album = c("Section 80", "DAMN."))
# Iterate through the albums and artists using a map call
album_lyrics <- kdot_albums %>%
mutate(tracks = map2(artist, album, genius_album))