Skip to content

Instantly share code, notes, and snippets.

View mbannert's full-sized avatar

Matt Bannert mbannert

View GitHub Profile
@mbannert
mbannert / read geo file
Created April 21, 2016 08:29
PostgreSQL Postgis R GeoJSON example
# Postgis data type in my case is WSG84: geom | geometry(Point,4326)
library(jsonlite)
library(RPostgreSQL)
source("dbConnect.R") # create some connection object called con
p <- dbGetQuery(con,
"SELECT id,
postal_code,
st_asGeoJSON(geom)
FROM sometable
library(fmsb)
# all data taken from basketball-reference.com
curry_now <- read.table(text="Season,Age,Tm,Lg,Pos,G,GS,MP,FG,FGA,FG%,3P,3PA,3P%,2P,2PA,2P%,eFG%,FT,FTA,FT%,ORB,DRB,TRB,AST,STL,BLK,TOV,PF,PTS
2015-16,27,GSW,NBA,PG,20,20,34.3,10.6,20.2,.524,5.1,11.1,.459,5.5,9.1,.602,.650,5.8,6.2,.943,0.5,4.6,5.1,6.0,2.4,0.2,3.6,1.7,32.0",sep=",",dec=".",header=T)
jordan_93 <- read.table(text="Season,Age,Tm,Lg,Pos,G,GS,MP,FG,FGA,FG%,3P,3PA,3P%,2P,2PA,2P%,eFG%,FT,FTA,FT%,ORB,DRB,TRB,AST,STL,BLK,TOV,PF,PTS
1992-93,29,CHI,NBA,SG,78,78,39.3,12.7,25.7,.495,1.0,2.9,.352,11.7,22.7,.514,.515,6.1,7.3,.837,1.7,5.0,6.7,5.5,2.8,0.8,2.7,2.4,32.6
",
header = T,sep=",",dec=".")
# load packages - install from CRAN if you don't have the package:
# install.packages("waffle")
library(waffle)
# all data taken from
# basketball-reference.com - keep up the great work!
dubs <- c("X3p" = 251,"X2p" = 552 ,"FT" = 333)
dubs_points_scored <- dubs * c(3,2,1)
names(dubs_points_scored) <- c("by 3s","by 2s","by FTs")
@mbannert
mbannert / pca_example.R
Created November 6, 2015 14:39
made up example list wise PCA with AIC
# just an implementation example that does not make a whole lot of sense...
library(MASS)
y <- mtcars$mpg
ll <- list(cars1 = mtcars[,-1], cars2 = mtcars[,-1]*100)
out_put_list <- lapply(ll,function(x){
pca <- prcomp(x,scale.=T)
pca_scores <- as.data.frame(pca$x)
# maybe give back the result and do it
# multiple dfs
@mbannert
mbannert / fxRate.R
Created June 4, 2015 20:06
CHFEUR FxRate
library(RJSONIO)
library(RCurl)
fx <- getURL("http://www.quandl.com/api/v1/datasets/BOE/XUDLBK68")
fx_df <- data.frame(date = as.Date(unlist(lapply(fromJSON(fx)$data,"[[",1))),
EURCHF = unlist(lapply(fromJSON(fx)$data,"[[",2)))
# save("fx_df",file="presentation/data/fx_df_temp.RData")
plot(fx_df,main = "Swiss Franc per Euro Exchange Rate",
xlab = "Source: Quandl API (BOE)",ylab = "",type="l")
@mbannert
mbannert / RpostgreSQL-patch.R
Created February 16, 2015 16:28
dbIsValid method for RPostgreSQL
setMethod("dbIsValid", "PostgreSQLConnection", function(dbObj, ...) {
isValid <- tryCatch({dbGetInfo(dbObj)},
error = function(e) NULL)
!is.null(isValid)
})
@mbannert
mbannert / patch_getfame.R
Created February 11, 2015 21:12
Patch to make TSget still work with fame when loading data.table
# getfame makes use of year from the tis package...
# and thus conflicts with data.table... if the fame package would import instead of
# depending on tis, everything would be solved... but it doesn't so this
# patch helps out.
# Hat tip to Richie cotton for helping me out in this SO thread
# http://stackoverflow.com/questions/26462903/how-to-handle-masking-conflicts-in-r-package-the-right-way
# The remaining problem with Richie's ideas was that getfame contains numerous not exported
# function so I had to hunt them down and add a fame:::functionname to it...
# here's the patch, TSget won't fail after loading data.table. YAY!
assignInNamespace(
@mbannert
mbannert / rgb2hex.R
Created December 4, 2014 15:21
Convert RGB to hex in R
rgb2hex <- function(r,g,b) sprintf('#%s',paste(as.hexmode(c(r,g,b)),collapse = ''))
rgb2hex(255,0,0)
# returns '#ff0000'
@mbannert
mbannert / split_subsets_example.R
Created November 29, 2014 11:01
reproducible example to split dataset into intervals when sorted
# generate data, variables will be name X1 .... X8
# dimension: 1200 rows, 7 cols
daten <- data.frame(matrix(rnorm(8400),ncol = 7))
# 8th col is just sequential id 1-1200
daten$id <- as.numeric(row.names(daten))
# assume data are sorted,
# add another column that indicates
# the subset
@mbannert
mbannert / gist:e62079c55a493e7c6668
Created October 3, 2014 16:16
Improvement of the R code used in Ioannis' example
# load data from an SPSS file
library(foreign)
votematch<-read.spss("ioannis.sav", to.data.frame=TRUE, reencode="UTF-8")
# explore the loaded dataset
head(votematch)
dim(votematch)
names(votematch)
str(votematch)