Skip to content

Instantly share code, notes, and snippets.

View Plaudenslager's full-sized avatar

Peter Laudenslager Plaudenslager

View GitHub Profile
@Plaudenslager
Plaudenslager / Date_conversion.r
Created May 12, 2016 03:26
Convert string column into Date format in R
library(dplyr)
# raw_data is a dataframe; probably imported from CSV or other file
# clean_data is the dataframe we intend to use in the analysis
# Signup.Date is a string that contains the date - in this case the format is "mm/dd/yyyy"
clean_data <- mutate(raw_data, Signup.Date = as.Date(Signup.Date, "%m/%d/%y"))
# Load dataset
setwd("~/Dropbox/work")
raw_data <- read.csv("2016-05-11 Trial Download Detail.csv", stringsAsFactors = FALSE)
str(raw_data)
head(raw_data)
tail(raw_data)
@Plaudenslager
Plaudenslager / FiscalQtr.r
Created May 12, 2016 03:18
Convert dates to fiscal quarter [R function]
fiscal_Qtr <- function(date, first_month_in_FY=4){
month = as.numeric(format(date, "%m"))
year = as.numeric(format(date, "%y"))
if (month < first_month_in_FY){
fiscal_year = year
quarter = (13 - first_month_in_FY + month-1) %/% 3 + 1
}else{
fiscal_year = year + 1
quarter = (month - first_month_in_FY) %/% 3 + 1
}
@Plaudenslager
Plaudenslager / expanded_stl.r
Created May 15, 2015 18:44
Add raw sales data & date columns to a standard stl (seasonal-trend decomposition) data.frame
expanded_stl <- function(ts_data, sales_data, dates){
# requires ts data, plus the raw sales data and dates vectors
# returns a data.frame suitable for plotting with stl_plotter
data.stl <- stl(ts_data, s.window = 11, t.jump = 1)
ggdata <- data.frame(data.stl$time.series)
ggdata$sales <- sales_data
ggdata$Date <- dates
#ggmac.stl$Product <- "Mac"
return(ggdata)
@Plaudenslager
Plaudenslager / stl_plotter.r
Last active August 29, 2015 14:21
standardized plot function for seasonal data
stl_plotter <- function(title, data){
# requires dataframe from expanded_stl
# plots graphics to files
theme_set(theme_minimal())
p <- ggplot(data, aes(x=Date, y=sales))
p1 <- p + geom_line() + ylab("Data")
p <- ggplot(data, aes(x=Date, y=trend))
@Plaudenslager
Plaudenslager / MyColors.r
Last active August 29, 2015 14:20
Color-blind friendly palette for R projects
##' Color-blind friendly palette
##' From \url{http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/}
##' @title Color-blind friendly palette
##' @param palette Choose "cb", "rcb", or "bly". cb is the Winston
##' Chang color blind palette; rcb is that palette in reverse; bly
##' puts the yellow and blue in the palette first.
##' @return Variations on an eight-color color-blind friendly palette.
##' @author Winston Chang / Kieran Healy
##' @export
my.colors <- function(palette="cb"){

Keybase proof

I hereby claim:

  • I am plaudenslager on github.
  • I am peterl (https://keybase.io/peterl) on keybase.
  • I have a public key whose fingerprint is 49F8 140B F2A9 7183 480A 8AA8 1D66 F371 9A26 033D

To claim this, I am signing this object: