Skip to content

Instantly share code, notes, and snippets.

View Ram-N's full-sized avatar

Ram Narasimhan Ram-N

View GitHub Profile
@Ram-N
Ram-N / multiple_gsub.R
Created April 19, 2016 04:57
R multiple Grid substituitions.
mgsub <- function(pattern, replacement, x) {
if (length(pattern)!=length(replacement)) {
stop("pattern and replacement do not have the same length.")
}
result <- x
for (i in 1:length(pattern)) {
result <- gsub(pattern[i], replacement[i], result)
}
result
}
@Ram-N
Ram-N / ggVis_selectize_multiple.R
Created April 18, 2016 04:30
An example of using ggVis selectize with multiple=TRUE in Shiny.
library(ggvis)
library(shiny)
#Example of Selectize, with Multiple = TRUE
ui = shinyUI(pageWithSidebar(
div(),
sidebarPanel(
sliderInput("n", "Number of points", min = 1, max = nrow(mtcars),
value = 10, step = 1),
@Ram-N
Ram-N / hexbin.R
Created March 28, 2016 00:42
Deconstructing Hexbix US map plot
#understanding the code behind Bob Rudis' blog post
#https://rud.is/b/2015/05/14/geojson-hexagonal-statebins-in-r/
rm(list=ls())
library(rgdal)
library(rgeos)
require(maptools)
setwd("~/RStats/data")
ogrInfo("us_states_hexgrid.geojson", "OGRGeoJSON")
@Ram-N
Ram-N / lm_rand.R
Last active March 13, 2016 18:58
LM with a few randomly selected columns
one_lm_attempt <- function (df, target) {
rnd5 <- sample(10,5)
predictor_variables <- names(df)[rnd5]
#create the formula, string to give to lm()
rnd_formula <- formula(paste(target, " ~ ",
paste(predictor_variables, collapse=" + ")))
m <- lm(rnd_formula, df)
#print (summary(m)$r.squared)
#print (summary(m)$adj.r.squared)
return(c(rnd5, summary(m)$r.squared,summary(m)$adj.r.squared ))
@Ram-N
Ram-N / server.R
Created September 5, 2013 19:32
Response to StackOverflow R (Shiny) question around Automating interaction. (2 files)
library(shiny)
updates <- 0
updater <- function(updates){ updates + 1 }
shinyServer(function(input,output, session){
updateTracker <- reactive( {
invalidateLater(as.numeric(input$secPause) * 1000, session)
@Ram-N
Ram-N / kk_plyr.R
Last active December 20, 2015 22:39
# @ Given a data frame slice, this function determines who had more wins in that slice
moreWins <- function(df) {
valid.rows <- sum(!is.na(df$GK.won)) # count of 0 or 1 values
if(valid.rows==0) {
return("00")
}
gk.wins <- sum(na.omit(df$GK.won == "1")) #how many times did GK win
gk.losses <- sum(na.omit(df$GK.won == "0")) #how many times did GK lose
if(gk.wins > gk.losses) {
@Ram-N
Ram-N / k_vs_k_plotting.R
Last active December 20, 2015 22:39
Code to Visualize "Pianograms" - Winner-take-all type plots given two or more players. In this example, we plot Kasparov vs Karpov's Wins - sliced yearly, 5-year and decade-wise buckets. Code accompanies this blog post:
rm(list=ls())
library(ggplot2)
library(plyr)
library(reshape2)
df <- read.csv("~/RStats/data/KvsK_lifetime.csv", stringsAsFactors=F, strip.white=T)
df <- arrange(df, game) #use plyr's arrange to Sort by Game Number
#For ease drop the Opening, Event.Locale columns and Moves Columns
@Ram-N
Ram-N / multi_knapsack.R
Created August 6, 2013 06:53
An Integer Programming based solution to a multi-Knapsack problem posted in StackOverflow. This solution uses R and in particular the lpSolveAPI package. The question can be found at: http://stackoverflow.com/questions/18036966/algorithm-for-combining-numbers-into-optimal-groups-which-do-not-exceed-a-vector
library(lpSolveAPI)
library(reshape2)
library(ggplot2)
#Problem definition
trim.lengths <- c(44, 57, 86, 86, 40, 88, 88, 41, 40, 40, 62, 62, 54, 54, 55, 55, 63, 63, 75, 75, 100, 100)
avail.lengths <- c(120, 103, rep(100, 9), 98, rep(97, 4), 95, rep(88, 3), 85, 65)
num.trims = length(trim_lengths) #22
num.avail = length(avail.lengths) #22
init(side, num.colors) #df, const.type.vec have been initialized
df <- populate.Amatrix() #see github link for this function
rhs.vector <- create_rhs_vector(rhs.vector, terminal.cells)
const.type.vec <- createConstraintTypeVector(const.type.vec)
length(rhs.vector); length(const.type.vec)
@Ram-N
Ram-N / problem.R
Last active December 19, 2015 06:49
terminal.cells <- read.csv(problemfile, header=T, stringsAsFactors=FALSE)
terminal.cells$tcell <- (terminal.cells$Y -1)* side + terminal.cells$X # cell serial number
num.colors <- length(unique(terminal.cells$color))
colorpalette <- unique(terminal.cells$palette)
#The problemfile
#X,Y,color, palette
#1,5,1,red
#2,4,1,red