Skip to content

Instantly share code, notes, and snippets.

Avatar

Sam Firke sfirke

View GitHub Profile
@sfirke
sfirke / gist:c0bd2b9c4d4e044b040966841e19a73b
Last active Oct 19, 2016
quick hack at get_fuzzy_dupes() function
View gist:c0bd2b9c4d4e044b040966841e19a73b
library(pacman)
p_load(fuzzyjoin, dplyr)
# returns clusters of records that almost match
get_fuzzy_dupes <- function(x, max_dist = 2){
result <- stringdist_inner_join(x, x, max_dist = max_dist, distance_col = "distance")
result <- result[result[[1]] != result[[2]], ] # remove actual 100% accurate duplicates
result <- t(apply(result, 1, sort)) # these two lines treat A, B as a duplicate of B, A and remove it. From http://stackoverflow.com/a/9028416
result <- result[!duplicated(result), ]
as_data_frame(result) %>%
@sfirke
sfirke / email_split.R
Created Jul 5, 2016
separating first and last names in email
View email_split.R
library(stringr)
get_part_before_dot <- function(email){
x <- str_split(email, "[.]")
lapply(x, `[[`, 1) %>%
unlist
}
dat <- data.frame(email = c("robert.rosen@tntp.org", "Sam.firke@tntp.org"))
@sfirke
sfirke / date_to_sy.R
Created Jul 5, 2016
Function for turning Date into school year string
View date_to_sy.R
## Date to SY function
## Year of 2nd argument does not matter
## Turns 2015-10-02 into "2015-16", and 2016-04-05 into "2015-16", with cutoff day = 2010-07-01
date_to_sy <- function(date_var, last_day_of_sy){
if(!(is.Date(date_var) & is.Date(last_day_of_sy))){stop("`date_var` and `last_day_of_sy` must both be class Date")}
cutoff_day <- day(last_day_of_sy)
cutoff_month <- month(last_day_of_sy)
case_when(
is.na(date_var) ~ as.character(NA),
month(date_var) > cutoff_month ~ paste0(year(date_var), " - ", year(date_var) + 1), # if past cutoff, X - X+!
@sfirke
sfirke / clean_names.R
Created Jan 29, 2016
Cleaning data.frame names with dplyr
View clean_names.R
clean_names <- function(dat){
# Takes a data.frame, returns the same data frame with cleaned names
old_names <- names(dat)
new_names <- old_names %>%
gsub("%", "percent", .) %>%
make.names(.) %>%
gsub("[.]+", "_", .) %>%
tolower(.) %>%
gsub("_$", "", .)
setNames(dat, new_names)
@sfirke
sfirke / gist:e9bd0c41f5105bc540d5
Created Oct 9, 2015
StackOverflow SEDE query for set.seed() values
View gist:e9bd0c41f5105bc540d5
SELECT Id, Body, Tags
FROM Posts
WHERE Body LIKE '%set.seed(%' AND Tags LIKE '%r%'