Skip to content

Instantly share code, notes, and snippets.

library(dplyr)
library(ggplot2)
iris %>%
group_by(Species) %>%
summarize(
lower=quantile(Sepal.Length, .25),
upper=quantile(Sepal.Length, .75),
med=median(Sepal.Length),
avg=mean(Sepal.Length),
@mplourde
mplourde / diag_behavior.scala
Last active May 23, 2016 19:19
SystemML diag behavior issue
/*
In this example I create two matrices in the same manner, extract the diagonal, and then turn the nx1
matrix of the diagonal values into a diagonal matrix by calling `diag` again. When the initial matrix contains all 1s,
it works as expected. However, when the initial matrix contains all zeros, the second `diag` does not convert the nx1
matrix of diagonal values to a diagonal matrix. I can work around this behavior, but I thought I should bring it to your attention.
*/
val systemml_alg =
"""
fileOut1 = ""
sql_query(cnxn, "
SELECT MIN(media_week_id) 'min_media_week',
MAX(media_week_id) 'max_media_week'
FROM availability_estimation_inputs
")
#' @export
add_time_sig <- function(D) {
daypart_cols <- names(D)[grepl('hour_', names(D))]
# NOT CURRENTLY USED
#D <- D %>%
# mutate_(
# week_sig = ~ifelse(get_media_month(log_date) != get_media_month(detail_start),
# get_media_week(detail_end),
get_p_id <- function(p_id, connection) {
sqlQuery(connection, sprintf(
"select p.id 'proposal_id',
pd.id 'proposal_detail_id',
mm.media_month,
b.name 'MVPD',
n.code 'network',
mpi.post_log_recieved_date 'log_date',
pd.start_date 'detail_start',
pd.end_date 'detail_end',
@mplourde
mplourde / varsel.R
Created August 22, 2015 23:22
VARselect
vsel <- function (y, lag.max = 10, type = c("const", "trend", "both",
"none"), season = NULL, exogen = NULL)
{
browser()
y <- as.matrix(y)
if (any(is.na(y)))
stop("\nNAs in y.\n")
colnames(y) <- make.names(colnames(y))
K <- ncol(y)
lag.max <- abs(as.integer(lag.max))
@mplourde
mplourde / gist:f068ee466f5b7af906f8
Last active March 10, 2016 20:27
0 to cluster with notebook
# create instance, create key, install awscli, do awscli configure,
export AWS_ACCESS_KEY_ID=XX
export AWS_SECRET_ACCESS_KEY=XX
export SPARK_HOME=~/spark-1.4.1-bin-hadoop2.6
$SPARK_HOME/ec2/spark-ec2 --key-pair=XX \
--instance-type=m1.large \
--zone us-east-1a \
--identity-file=XX.pem \
@mplourde
mplourde / ggplot top Y-axis
Created February 25, 2015 19:00
ggplot top Y-axis
x <- ggplot(mtcars, aes(x=mpg, y=disp, label=carb)) +
geom_point() +
geom_text(y=Inf, vjust=-.3)
g <- ggplotGrob(x)
g$layout$clip[g$layout$name == "panel"] = "off"
grid.draw(g)
@mplourde
mplourde / R shiny dynamic outputs
Created December 12, 2014 15:08
R shiny dynamic outputs
ui <- fluidPage(
selectInput('plot_type', 'Plot type', choices=c('point', 'line')),
sliderInput('n', 'N plots', value=1, min=1, max=100),
uiOutput('plots')
)
server <- function(input, output, session) {
output$plots <- renderUI({
ptype <- input$plot_type
@mplourde
mplourde / Working with layouts in grid
Created December 9, 2014 19:00
Working with layouts in grid
# example1 (using grobs instead of grid.*)
vp <- vpTree(
parent=viewport(layout=grid.layout(nrow=2, ncol=2), name='parent'),
children=vpList(
viewport(layout.pos.row=1, layout.pos.col=1, name='vp1'),
viewport(layout.pos.row=1, layout.pos.col=2, name='vp2')
)
)
pushViewport(vp)
seekViewport('parent')