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 / ColNames.R
Created May 30, 2018 17:58
Using paste0 with length=option
i <- 1:50
b <- 1:5
ColNames <- paste0("X_", i, "_", rep(b, each=length(i)))
df[, !(colnames(df) %in% c("x","bar","foo"))]
import pandas as pd
df = pd.DataFrame([
['2018-03-11', '11:03', 86],
['2018-03-14', '09:27', 43],
['2018-03-14', '12:08', 22],
['2018-03-15', '03:13', 140],
['2018-03-17', '18:06', 10]],
columns=['Date','Time','Duration'])
@Ram-N
Ram-N / facet_annotate.R
Last active January 12, 2018 05:58
Adding Annotations to Facet_Grid
mtcars[, c("cyl", "am", "gear")] <- lapply(mtcars[, c("cyl", "am", "gear")], as.factor)
p <- ggplot(mtcars, aes(mpg, wt, group = cyl)) +
geom_line(aes(color=cyl)) +
geom_point(aes(shape=cyl)) +
facet_grid(gear ~ am) +
theme_bw()
p
@Ram-N
Ram-N / data_manipu.R
Created January 5, 2018 05:20
TIDYR - gather, unite and spread.
library(tidyr)
df <- data.frame(
MKT=c("ABIORD", "ABIBOS", "GCFPQR", "ABIORD", "ABIBOS", "GCFPQR", "ABIORD", "ABIBOS", "GCFPQR"),
VERSION=c(1,2,3,4,1,2,3,4,1),
REVERSALS=c(7,6,7,7,7,6,7,7, 9),
NEIGH=c(1,2.3,3.3,4,3.3,2,1,3.3,4.3),
DIFFMETRIC=c(1.1,2.1,3.1,4.1,3.1,2.1,1.1,3.1,4.1))
@Ram-N
Ram-N / Loop Through
Created June 12, 2017 23:40
24_game_puzzle
def common_elements(list1, list2):
return list(set(list1) & set(list2))
import itertools
fileidx = 1
for x,y in itertools.combinations(cards, 2):
match = common_elements(x, y)
print(len(match), match)
if len(match):
library(dplyr)
df1 <- mtcars
df2 <- filter(mtcars, mpg >19)
names(df1)
col1 <- 'qsec'
val1 <- 15.5
col2 <- 'qsec'
@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")