Skip to content

Instantly share code, notes, and snippets.

``` r
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.3.2
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
@stillmatic
stillmatic / demo_exam_data.csv
Created March 16, 2017 13:56
demo exam data
playerID yearID stint teamID lgID G AB R H X2B X3B HR RBI SB CS BB SO IBB HBP SH SF GIDP
colboji01 1976 1 ML4 AL 32 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
goodmiv01 1941 1 CIN NL 42 149 14 40 5 2 1 12 1 NA 16 15 NA 1 2 NA 4
hoilech01 1994 1 BAL AL 99 332 45 82 10 0 19 53 2 0 63 73 2 5 1 4 6
pujolal01 2003 1 SLN NL 157 591 137 212 51 1 43 124 5 1 79 65 12 10 0 5 13
barnoed01 1965 1 BAL AL 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
@stillmatic
stillmatic / factor.R
Last active December 20, 2016 23:12
factors are hard
library(dplyr)
library(magrittr)
# dummy data
df <- data_frame(
a = c("1", "1", "2"),
b = c("a", "b", "c")
)
# convert to factor
@stillmatic
stillmatic / imdb_scrape.R
Last active December 19, 2016 21:24
for given movie has the director ever been nominated for an oscar
library(rvest)
library(dplyr)
library(magrittr)
urls <- c("http://www.imdb.com/title/tt1375666/",
"http://www.imdb.com/title/tt0499549/",
"http://www.imdb.com/title/tt6057032/")
dir_nom <- function(u) {
movie <- read_html(u)
@stillmatic
stillmatic / runway_scrape.R
Last active December 19, 2016 21:28
scrapes rent the runway to get dress rating info
library(rvest)
library(magrittr)
library(dplyr)
#RTR Dresses All Pages Attempt 2 - Worked
#page 1 url = https://www.renttherunway.com/products/dress?action=click_all_dresses&nav_location=mainmenu&object_type=top_nav
#page 2 url = https://www.renttherunway.com/products/dress?action=click_all_dresses&nav_location=mainmenu&object_type=top_nav&page=2
urlsRTRDresses <- paste0(
"https://www.renttherunway.com/products/dress?page=", 1:59
)
@stillmatic
stillmatic / kmeans.R
Last active November 2, 2016 02:42
K-Means
# written during an interview
# assumes your data is in coords
library(dplyr)
set.seed(1)
k <- 2
initial_points <- base::sample(nrow(coords), k)
euclid <- function(one, two) {
@stillmatic
stillmatic / simulation.R
Created September 25, 2016 21:14
hw1 simulation
simulate_lm <- function(n) {
# note: `n` is an input but not used (don't worry about this hack)
x <- seq(0, 1, length = 40)
y <- 1 + 1.2 * x + rnorm(40, sd = 2)
t_star <- qt(0.975, 38)
lse <- lm(y ~ x)
lse_out <- summary(lse)$coefficients
se <- lse_out[2, 2]
b1 <- lse_out[2, 1]
upper_CI = b1 + t_star * se
@stillmatic
stillmatic / fizzbuzz.R
Created August 24, 2016 17:24
acceptably R-like implementation of fizzbuzz
# at least they're not loops
fizzbuzz <- function(x) {
mod_five <- (x %% 5) == 0
if(x %% 3 == 0) {
if(mod_five) return("fizzbuzz")
else return("fizz")
}
if(mod_five) return("buzz")
return(x)

Keybase proof

I hereby claim:

  • I am stillmatic on github.
  • I am hua (https://keybase.io/hua) on keybase.
  • I have a public key whose fingerprint is 6334 1815 CDA1 CE92 9D08 90F8 1EA6 17AD CD6D 0FBF

To claim this, I am signing this object:

@stillmatic
stillmatic / osx_notification.R
Created July 28, 2016 04:24
Wrapper function to easily throw notifications on OS X.
#' Wrapper function to throw OS X notifications.
#'
#' @title OSX notifier
#' @author \href{https://github.com/stillmatic}{Chris Hua}
#'
#' @param message Body text of notification
#' @param title Title of notification
#' @param sound Optional. Valid sounds are in `~/Library/Sounds` or `/System/Library/Sounds`
#'
#' @return an AppleScript notification