Skip to content

Instantly share code, notes, and snippets.

View abresler's full-sized avatar

Alex Bresler abresler

View GitHub Profile
## Warren Buffet's 1B Basketball Challenge ##
expected_value <- function(p,ngames=63,prize=1000000000){
p^ngames * prize
}
## What is the expected value of an entry
## given a particular level of prediction accuracy
expected_value(p=0.80)
expected_value(p=0.85)

Although very handy and useful, I think most new R developers do not even know that tables exist. They are a little tricky to use, but they offer some nice functionality. These tables become even more useful when we can visualize them as networks. This little bit of code is a rough first attempt at converting tables to network nodes and edges.

live example

@abresler
abresler / gist:3249776
Created August 3, 2012 17:26 — forked from christophergandrud/gist:1284498
Simple Web Crawler for Text
library(foreign)
library(RCurl)
addresses <- read.csv("~/links.csv") # Create a .csv file with all of the links you want to crawl
for (i in addresses) full.text <- getURL(i)
text.sub <- gsub("<.+?>", "", full.text) # Removes HTML tags
text <- data.frame(text.sub)
@abresler
abresler / gist:3249788
Created August 3, 2012 17:27 — forked from christophergandrud/gist:1287427
Google Motion Chart of World Bank External Debt Data
# Create Google Motion Chart from World Bank Finance Data
# Inspired by http://lamages.blogspot.com/2011/09/accessing-and-plotting-world-bank-data.html
# Written by Christopher Gandrud
# 15 October 2011
library(WDI)
library(gregmisc)
library(googleVis)
## Download websites
addresses <- read.csv("~/links.full.csv")
for (i in addresses){
fed.text <- getURL(i)
}
fed.df <- as.data.frame(fed.text)
## Download websites
addresses <- read.csv("~/links.full.csv")
for (i in addresses){
fed.text <- getURL(i)
}
fed.df <- as.data.frame(fed.text)
@abresler
abresler / price_and_volume.R
Created October 27, 2012 20:10 — forked from dsparks/price_and_volume.R
Scraping and plotting InTrade data
# Scraping and plotting InTrade data
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ggplot2", "lubridate")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Fin the "contractId" of the contract in which you're interested
# Let's try Barack Obama to win the third Presidential debate:
# http://www.intrade.com/v4/markets/contract/?contractId=766623
contractID <- 766623 # second debate is 766621
@abresler
abresler / Background_lines.R
Created October 27, 2012 20:23 — forked from dsparks/Background_lines.R
Faceted plots with reference geometries
# Better alternatives to 3-D
# Here are some examples of what we don't want to do:
# http://stackoverflow.com/questions/12765497/create-a-ribbon-plot
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Air passenger data. ts converted to long matrix:
@abresler
abresler / twitter_search.R
Created December 9, 2012 02:36 — forked from dsparks/twitter_search.R
Twitter search
doInstall <- TRUE
toInstall <- c("twitteR", "lubridate")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
searchTerms <- c("New York", "Los Angeles", "Chicago", "Houston", "Philadelphia",
"Phoenix", "San Antonio", "San Diego", "Dallas", "San Jose",
"Jacksonville", "Indianapolis", "Austin", "San Francisco",
"Columbus", "Fort Worth", "Charlotte", "Detroit", "El Paso",
"Memphis")
@abresler
abresler / diagrammer-bball-vis.R
Last active December 18, 2015 15:11
Simple visualization of NBA player movements using the DiagrammeR R package
library(DiagrammeR)
library(magrittr)
library(visNetwork)
moments <-
'https://gist.githubusercontent.com/rich-iannone/9c4e50cd5d811b841349/raw/65bc97ddac2f9e52a4252fcf2060007fe1afeb7d/moment.csv' %>%
read_csv()
# Generate the court
court <-