Skip to content

Instantly share code, notes, and snippets.

View vikjam's full-sized avatar
💭
👨🏾‍💻

vikjam

💭
👨🏾‍💻
View GitHub Profile
@vikjam
vikjam / select.sh
Created March 18, 2017 14:33
Use awk to subset a CSV
# http://stackoverflow.com/questions/6491532/how-to-subset-a-file-select-a-numbers-of-rows-or-columns
cat largefile | awk 'NR >= 10000 && NR <= 100000 { print }'
@vikjam
vikjam / locus.r
Created June 30, 2017 11:45
Quick comparison of locus of control by debt.
library(foreign)
library(dplyr)
library(ggplot2)
success <- read.dta("ALP_MS118_2016_03_13_06_51_42.dta")
scpc2010 <- read.dta("SCPC2010.dta")
scpc2010.adopt <- scpc2010[ , c("prim_key", "cc_adopt", "cc_sh")]
success.locus <- success[ , c("ms118_prim_key", "ms118_lc1")]
@vikjam
vikjam / prison.r
Created June 30, 2017 11:46
Quick plot of prison rates versus murder rate.
library(rvest)
library(ggvis)
wiki.prison <- html("http://en.wikipedia.org/wiki/List_of_countries_by_incarceration_rate")
wiki.prison.data <- wiki.prison %>%
html_nodes("table") %>%
html_nodes("table") %>%
.[[2]] %>%
html_table(fill = TRUE)
@vikjam
vikjam / treas.r
Created June 30, 2017 11:48
Plot of yields
library(rvest)
library(ggplot2)
library(ggthemes)
library(MASS)
library(splines)
treas.html <- html("http://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yield")
yield.data <- treas.html %>%
html_nodes("table") %>%
.[[67]] %>%
@vikjam
vikjam / nytimes.py
Created June 30, 2017 13:39
NYT Headline Grabber
#!/usr/bin/env python
from lxml import html
import requests
import nltk
page = requests.get('http://nytimes.com')
tree = html.fromstring(page.text)
top_stories = tree.xpath('//*[@id="top-news"]/*//h2/a/text()')
more_stories = tree.xpath('//*[@id="main"]/div[10]/*//article/h2/a/text()')
all_stories = top_stories + more_stories
@vikjam
vikjam / rename.rb
Last active July 31, 2017 14:36
Quick renaming in Ruby
Dir.glob('*.mp3') do | the_filename |
track = the_filename[0..2].strip()
File.rename(the_filename, "05-#{track}.mp3")
end
@vikjam
vikjam / nonlinear.jl
Created October 28, 2017 19:57
Nonlinear solver
using JuMP, Ipopt
A = 1; δ = 0.1; θ = 0.3; β = 0.96; ɛ = 0.5; χ = 0.5;
r = (1 / β) - (1 - δ);
m = Model(solver = IpoptSolver())
@variable(m, 0 <= k <= Inf)
@variable(m, 0 <= n <= Inf)
@variable(m, 0 <= w <= Inf)
@vikjam
vikjam / install.sh
Created March 3, 2018 18:08
Academic Mac
brew tap caskroom/cask
brew install brew-cask
brew install Caskroom/cask/xquartz
brew cask install java
brew tap homebrew/science
brew install R
brew install Caskroom/cask/rstudio
@vikjam
vikjam / copy_exif.rb
Last active January 21, 2019 17:11
Copy EXIF info from one photo to another
require 'mini_exiftool'
photo = MiniExiftool.new('new.jpg')
photo.tags.sort.each do |tag|
photo.copy_tags_from('original.jpg', tag)
end
photo.save
@vikjam
vikjam / output.Rout
Last active February 10, 2019 19:36
Summarize dplyr
Wrong mean: 5.84333333333333
Right mean: 5.936
The wrong mean didn't filter: 5.84333333333333