Skip to content

Instantly share code, notes, and snippets.

View patternproject's full-sized avatar

patternproject

View GitHub Profile
#### Load Data ####
df.winners = read.csv(
"winnersWithMean.csv",
header = TRUE,
stringsAsFactors = FALSE,
na.strings = c("", "NA")
)
# dput for df.winners
# at the end of the code
@patternproject
patternproject / glitchulate.py
Created August 27, 2016 10:34 — forked from hawkowl/glitchulate.py
Make your favourite images into glitched monstrosities! (Requires pillow from PyPI, Py2/3/pypy compatible)
from __future__ import print_function
import sys
import random
from collections import deque
from PIL import Image
# THIS IS THE MAGIC VALUE THAT MAKES EVERYTHING GLITCHED
@patternproject
patternproject / saveListOfDfAsCsv.r
Created September 27, 2016 03:55
How to save a list of data frames
# list of all data frames
s.df = names(which(sapply(.GlobalEnv, is.data.frame)))
# list of data frames with .p. in their names
s.df.p = s.df[str_detect(s.df,regex("\\.p\\.",ignore_case = FALSE))]
# creating path
paths <- paste0(s.df.p,".1", ".csv")
# helper function
@patternproject
patternproject / pmap-v1.r
Last active October 17, 2016 11:17
df (df.nbl.win.2) contains country, yr1, y2 and yr3. Helper / Workhorse fn (fn_get_rank) takes country and year, returning rank. Using pmap I need to rename cols for proper mapping in pmap.
# -------------------
# HELPER FUNCTION
# -------------------
# fn to search rank
# INPUT: year (integer)
# OUTPUT: rank (integer)
# SRC:
# http://adv-r.had.co.nz/Functions.html
# https://cran.r-project.org/web/packages/ArgumentCheck/vignettes/ArgumentChecking.html
Social Strata Reason Rate
Rich people good education, high qualification 28%
Middle class good education, high qualification 33%
Poor good education, high qualification 18%
Rich people cunning, cheating 11%
Middle class cunning, cheating 21%
Poor cunning, cheating 32%
Rich people abilities, talents 13%
Middle class abilities, talents 8%
Poor abilities, talents 7%
@patternproject
patternproject / .block
Last active April 9, 2017 11:17
d3.challenge1.2017Apr09
license: mit
## `````````````````````````````````````````````
#### Read Me ####
## `````````````````````````````````````````````
## Trying to implement "sessionization" in R
## Details here:
## https://www.dataiku.com/learn/guide/code/reshaping_data/sessionization.html
## `````````````````````````````````````````````
## `````````````````````````````````````````````
#### Load Libraries ####
@patternproject
patternproject / gist:0a7ade8fa3d85453076d9bafc2087127
Created April 27, 2017 14:58
How to successively combine years and months using purrr
Hi,
-----------------------------------
PROBLEM STATEMENT
-----------------------------------
I want to generate URLS for each month for each year as follows:
[...]
https://s3.amazonaws.com/data/201611.csv.zip
https://s3.amazonaws.com/data/201612.csv.zip
@patternproject
patternproject / different_iteration.r
Created May 6, 2017 23:46
purrr - doing something different in first iteration
# Helper Function
fn_downloadZip <- function(URL) {
myzip <- unz(URL, filename=basename(URL))
c.zip.name = "test.zip"
c.file.path = file.path(c.home.dir,c.data.dir,c.zip.name)
c.data.folder = file.path(c.home.dir,c.data.dir)
download.file(URL, destfile=c.file.path)
f.name = unzip(c.file.path, exdir=c.data.folder)
# The main idea comes from this post:
# http://zevross.com/blog/2014/08/05/using-the-r-function-anti_join-to-find-unmatched-records/
# Setting up
df.1 <- data.frame(
state = rep(1:3, each=2),
country = rep(c("A","B"),3),
vals = rnorm(6)
)