Skip to content

Instantly share code, notes, and snippets.

View Ram-N's full-sized avatar

Ram Narasimhan Ram-N

View GitHub Profile
library("ggmap")
library(maptools)
library(maps)
visited <- c("SFO", "Chennai", "London", "Melbourne", "Johannesbury, SA")
ll.visited <- geocode(visited)
visit.x <- ll.visited$lon
visit.y <- ll.visited$lat
#> dput(visit.x)
@Ram-N
Ram-N / gist:5240959
Last active December 15, 2015 09:49
library(ggplot2)
library(reshape2)
data(movies)
movieGenres <- movies[c(18:23)] #subset to 6 genres
cor(movieGenres) # 6x6 cor matrix
#ggplot likes the data 'melted' one value per row
m <-melt(cor(movieGenres))
p <- ggplot(data=m, aes(x=Var1, y=Var2, fill=value)) + geom_tile()
@Ram-N
Ram-N / gist:5240937
Last active December 15, 2015 09:49
#set up a coloring scheme using colorRampPalette
red=rgb(1,0,0); green=rgb(0,1,0); blue=rgb(0,0,1); white=rgb(1,1,1)
RtoWrange<-colorRampPalette(c(red, white ) )
WtoGrange<-colorRampPalette(c(white, green) )
p <- p + scale_fill_gradient2(low=RtoWrange(100), mid=WtoGrange(100), high="gray")
@Ram-N
Ram-N / custom_coloring
Created March 25, 2013 21:21
Correlation Heatmaps in ggplot2 with custom colors
#set up a coloring scheme using colorRampPalette
red=rgb(1,0,0); green=rgb(0,1,0); blue=rgb(0,0,1); white=rgb(1,1,1)
RtoWrange<-colorRampPalette(c(red, white ) )
WtoGrange<-colorRampPalette(c(white, green) )
p <- p + scale_fill_gradient2(low=RtoWrange(100), mid=WtoGrange(100), high="gray")
p
@Ram-N
Ram-N / visitNode.R
Last active December 15, 2015 02:49
A simple function to recursively visit each node in a XML document. (Uses DT Lang's XML package.)
library(XML)
#Recursive Function to visit the XML tree (depth first)
visitNode <- function(node) {
if (is.null(node)) {
#leaf node reached. Turn back
return()
}
print(paste("Node: ", xmlName(node)))
num.children = xmlSize(node)
if(num.children == 0 ) {
@Ram-N
Ram-N / books.xml
Last active October 5, 2023 16:52
This is a sample XML file that Microsoft makes available.
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
@Ram-N
Ram-N / alleleSimulation.R
Created March 11, 2013 19:40
Given a population of AA, Aa, and aa genotypes, how do the two Alleles (A and a) propagate across generations?
rm(list=ls())
library(ggplot2)
library(reshape2)
#Function Definition
#Bookkeeping
calcAllelesInGeneration <- function(x) {
AA = sum(x==1)
AB = sum(x==2)
@Ram-N
Ram-N / gist:5136763
Last active December 14, 2015 19:29
#Bookkeeping
calcAllelesInGeneration <- function(x) {
AA = sum(x==1)
AB = sum(x==2)
BB = sum(x==3)
#print(unlist(x))
#print(c(AA, AB, BB))
num.A <- (2 * AA) + AB
num.B <- (2 * BB) + AB
return(c(num.A, num.B))
simulationOneTrial <- function(start.pop, knumGenerations, trial.index) {
df.allele <- NULL
df.gen <- NULL
#Keep track of the individuals in each generation
df.gen <- rbind(df.gen, start.pop)
x <- start.pop
for ( gen in 1:knumGenerations) {
getNextGen <- function(x) {
nextgen <- list()
for(i in seq(1, kStartPop, by=2)) {
firstborn <- getOffspring(x[i], x[i+1])
secondborn <- getOffspring(x[i], x[i+1])
nextgen[i] <- firstborn
nextgen[i+1] <- secondborn
}
return(unlist(nextgen))
}