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 / 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.
# RING of R-Rows C-cols in the center of width w cells
seedCenterRing <- function(r, c, wide){
for(x in as.integer((areaW-c)/2): as.integer((areaW + c)/2)){
for(y in as.integer((areaH-r)/2): as.integer((areaH + r)/2)){
area_df[x,y] <<- 1
}
}
#scoop out the inner ring
for(x in as.integer((areaW-c)/2 + wide): as.integer((areaW + c)/2 - wide)){
for(y in as.integer((areaH-r)/2+wide): as.integer((areaH + r)/2- wide )){
@Ram-N
Ram-N / gist:5019066
Last active December 14, 2015 03:09
# R-Rows C-cols in the center
seedAreaCenter <- function(r, c){
for(x in as.integer((areaW-c)/2): as.integer((areaW + c)/2)){
for(y in as.integer((areaH-r)/2): as.integer((areaH + r)/2)){
area_df[x,y] <<- 1
}
}
}
#Seed a central area and let it grow
# w-cols to the R and L of center column
seedColumns <- function(c, w){
startLCol = as.integer( (areaW-c) / 2 )
startRCol = as.integer( (areaW+c) / 2 )
for(y in 1:areaH) {
clist = c((startLCol-w):startLCol, startRCol:(startRCol+w))
lapply(clist, function(x) area_df[x,y] <<- 1)
}
}
@Ram-N
Ram-N / populationSimulation.R
Last active December 14, 2015 03:09
Simulating City Population Growth patterns with R
# Original Code by Matt Asher for statisticsblog.com
# Feel free to modify and redistribute, but please keep this notice
#Modifications by Ram Narasimhan
# @ramnarasimhan
source("~/RStats/simFunctions.R")
library(ggplot2)
library(plyr)
store_iteration_stats<- function(kNumSettlers, found.home, max.look.around) {
#how many settlers found homes
print(c(found.home," settled out of ", kNumSettlers, (found.home/kNumSettlers)*100, "%" ) )
#avg # of steps per new settler
print(c("num of look arounds max'ed:", max.look.around))
}
@Ram-N
Ram-N / gist:5068453
Last active December 14, 2015 09:59
# Are any of the 4 direct N E W S adjacent cells occupied?
isAnyOfNEWSCellsOccupied <- function(m,n) {
canOccupy <- FALSE
NEWSdir = c(2,4,5,7)
#traverse the vector of 4 elements
for(k in 1:4) {
xCheck = m + adjacells[NEWSdir[k],1]
yCheck = n + adjacells[NEWSdir[k],2]
if(!(outOfBounds(xCheck, yCheck))) {
if(area_df[xCheck, yCheck] > 0) {
# Are any of the 4 diagonally adjacent cells occupied?
isAnyDiagNeighboringCellOccupied <- function(m,n) {
canOccupy <- FALSE
diag.dir = c(1,3,6,8)
#traverse the vector of 4 elements
for(k in 1:4) {
xCheck = m + adjacells[diag.dir[k],1]
yCheck = n + adjacells[diag.dir[k],2]
if(!(outOfBounds(xCheck, yCheck))) {
# Make multiple runs (Replication of simulation) and take the average of stats
st <- data.frame()
st_row<- vector()
for(i in 1:kNumReplications) {
area_df <- resetIteration()
seedAreaWithPioneers(numPioneers,seeding.opt)
simstats <- accommodateSettlers(kNumSettlers, settling.option)
found.home <- simstats[1]
store_iteration_stats<- function(iter, kNumSettlers, found.home, max.look.around) {
#how many settlers found homes
perSet <- (found.home/kNumSettlers) * 100
print(c(found.home," settled out of ", kNumSettlers, perSet, "%" ) )
#avg # of steps per new settler
print(c("num of Settlers who hit max look around:", max.look.around))
return(c(iter, found.home, kNumSettlers, perSet))
}