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 / 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 / mgsub.R
Last active October 2, 2015 20:29
R - Simultaneously Replacing a set of columns using 'gsub'
library(ggplot2)
head(diamonds,10)
colList <- c("cut", "clarity") #list of columns to replace. Can also be column numbers.
rowstoReplace <- 1:10
lapply(colList, function(colname){gsub("I", ".", diamonds[rowstoReplace, colname])})
#lapply gives it one column name at a time...
#Use a comma instead of rowstoReplace if you want all rows changed.
@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 / google-research.R
Last active June 11, 2019 00:54
Google Research Grant recipients.
rm(list=ls())
library(ggplot2)
df <- read.csv("~/RStats/data/ResearchGrants2013.csv", stringsAsFactors=F, strip.white=T)
df <- df[,-6] #get rid of the trailing blank
dim(df)
#quick check
unique(df$Research.Category)
table(df$Research.Category)
@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
# actual problem definition
lpff <- make.lp(nrow=n.row, ncol=n.col)
defineIP()
lpff
solve(lpff)
sol <- get.variables(lpff)
defineIP <- function() {