Skip to content

Instantly share code, notes, and snippets.

View webbedfeet's full-sized avatar

Abhijit Dasgupta webbedfeet

View GitHub Profile
@webbedfeet
webbedfeet / rename_df.R
Created March 9, 2017 04:39
Renaming tables in a list-column
data(mtcars)
bl <- mtcars %>%
nest(-cyl) %>%
mutate(mod = map(data, ~lm(mpg~wt, data=.) %>% tidy()) %>%
mutate(mod = map(mod, ~setNames(., paste('mymod', names(.), sep='_')))) %>%
select(-data) %>%
unnest()
@webbedfeet
webbedfeet / insert_character.R
Created March 9, 2017 02:08
Inserting a character at a particular point in a string using R
# To put a particular character _ after the 10th position in a string
gsub('^([a-z]{10})([a-z]+)','\\1_\\2', x)
# or
library(stringr)
str_replace(x, '^([a-z]{10})([a-z]+]','\\1_\\2')
@webbedfeet
webbedfeet / whichfn.R
Created March 3, 2017 22:35
Identifying which package the currently accessible function is from
whichfn <- function(fun){
require(stringr)
str_match(capture.output(environment(fun)),'namespace:(\\w+)')[,2]
}
@webbedfeet
webbedfeet / ggkm.R
Created February 15, 2016 06:39 — forked from araastat/ggkm.R
Plotting a Kaplan-Meier curve using ggplot. ggkmTable.R adds a table below the plot showing numbers at risk at different times.
#’ Create a Kaplan-Meier plot using ggplot2
#’
#’ @param sfit a \code{\link[survival]{survfit}} object
#’ @param returns logical: if \code{TRUE}, return an ggplot object
#’ @param xlabs x-axis label
#’ @param ylabs y-axis label
#’ @param ystratalabs The strata labels. \code{Default = levels(summary(sfit)$strata)}
#’ @param ystrataname The legend name. Default = “Strata”
#’ @param timeby numeric: control the granularity along the time-axis
#’ @param main plot title
@webbedfeet
webbedfeet / credplot.R
Last active July 31, 2019 06:25
Forest plots using R and ggplot2
credplot.gg <- function(d){
# d is a data frame with 4 columns
# d$x gives variable names
# d$y gives center point
# d$ylo gives lower limits
# d$yhi gives upper limits
require(ggplot2)
p <- ggplot(d, aes(x=x, y=y, ymin=ylo, ymax=yhi))+
geom_pointrange()+
library(dplyr)
library(RPostgreSQL)
library(httr)
library(Lahman)
library(ggplot2)
# connect to the db
con <- src_postgres(dbname="harlan", host="localhost", user="harlan")
# upload the Batting db
#source MASTER
setwd("~/your/working/directory") #insert your working directory
#load libraries
library(rgdal)
library(maptools)
library(maps)
library(ggplot2)
library(plyr)
library(grid)
@webbedfeet
webbedfeet / dot_density.R
Created September 29, 2015 04:18 — forked from dsparks/dot_density.R
Dot Density Electoral Map
doInstall <- TRUE
toInstall <- c("XML", "maps", "ggplot2", "sp")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
myURL <- "http://en.wikipedia.org/wiki/United_States_presidential_election,_2012"
allTables <- readHTMLTable(myURL)
str(allTables) # Look at the allTables object to find the specific table we want
stateTable <- allTables[[14]] # We want the 14th table in the list (maybe 13th?)
@webbedfeet
webbedfeet / Makefile
Last active September 19, 2015 20:53 — forked from lmullen/Makefile
PDF slides and handouts using Pandoc and Beamer
SLIDES := $(patsubst %.md,%.md.slides.pdf,$(wildcard *.md))
HANDOUTS := $(patsubst %.md,%.md.handout.pdf,$(wildcard *.md))
all : $(SLIDES) $(HANDOUTS)
%.md.slides.pdf : %.md
pandoc $^ -t beamer --slide-level 2 -o $@
%.md.handout.pdf : %.md
pandoc $^ -t beamer --slide-level 2 -V handout -o $@
@webbedfeet
webbedfeet / pptx2pdf.py
Created September 1, 2015 15:45
Converting pptx to pdf in Windows using Python
import comtypes.client
def PPTtoPDF(inputFileName, outputFileName, formatType = 32):
powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
powerpoint.Visible = 1
if outputFileName[-3:] != 'pdf':
outputFileName = outputFileName + ".pdf"
deck = powerpoint.Presentations.Open(inputFileName)
deck.SaveAs(outputFileName, formatType) # formatType = 32 for ppt to pdf