Skip to content

Instantly share code, notes, and snippets.

View randyzwitch's full-sized avatar

Randy Zwitch randyzwitch

View GitHub Profile
@randyzwitch
randyzwitch / loop-try-1.R
Last active August 29, 2015 14:03
Funny Error Message
#Loop over df and accumulate results
parsed_df <- data.frame()
for(i in 1:nrow(df)){
temp <- cbind(df[i,],breakdown_list[[i]])
parsed_df <- rbind(parsed_df, temp)
}
There were 12 warnings (use warnings() to see them)
>warnings()
Warning messages:
@randyzwitch
randyzwitch / duplicate-name.R
Created June 26, 2014 14:53
Duplicate column names in data frame
#Loop over df and accumulate results
#Adding row.names = NULL fixes error message
parsed_df <- data.frame()
for(i in 1:nrow(df)){
temp <- cbind(df[i,],breakdown_list[[i]], row.names = NULL)
parsed_df <- rbind(parsed_df, temp)
}
names(parsed_df)
#Separate breakdown list and original data frame into different objects
df <- ex_df$report$data
breakdown_list <- df$breakdown
df$breakdown <- NULL
#Loop over df and accumulate results
parsed_df <- data.frame()
for(i in 1:nrow(df)){
right_df <- breakdown_list[[i]]
right_df <- rename(right_df, replace=c("name" = report_raw$report$elements$id[2]))
@randyzwitch
randyzwitch / julia-loop-datasets-query.jl
Created July 14, 2014 14:57
Looping over datasets in Julia, pulling sample
_
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: http://docs.julialang.org
_ _ _| |_ __ _ | Type "help()" to list help topics
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.3.0-prerelease+4028 (2014-07-02 23:42 UTC)
_/ |\__'_|_|_|\__'_| | Commit 2185bd1 (11 days old master)
|__/ | x86_64-w64-mingw32
julia> using ODBC
@randyzwitch
randyzwitch / sql-generation.jl
Last active August 29, 2015 14:03
Generating SQL code with Julia
#Starting portion of query, the groupby columns
julia> groupbycols ="select
interact.interactionid,
interact.agentname,
interact.agentid,
interact.agentgroup,
interact.agentsupervisor,
interact.sitename,
interact.dnis,
interact.agentextension,
@randyzwitch
randyzwitch / wordpress-chart.R
Last active August 29, 2015 14:04
Re-creating WordPress chart in R
library(ggplot2)
#Load Data
#Make sure months stay ordered - first time I ever wanted a factor!
#Label x-axis with every fifth label
visits_visitors <- read.csv("visits_visitors.csv")
visits_visitors$Month <- factor(visits_visitors$Month, levels = visits_visitors$Month, ordered = TRUE)
visits_visitors$Month_ <- ifelse(as.numeric(row.names(visits_visitors)) %% 5 == 0, as.character(visits_visitors$Month), "")
#Build plot as a series of elements
@randyzwitch
randyzwitch / wordpress-chart.py
Last active August 29, 2015 14:04
Re-creating WordPress chart using Python
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
#Read data into Python
dataset= pd.read_csv("visits_visitors.csv")
#Create every fifth month label
dataset["Month_"]= [value if (position+1) % 5 == 0 else "" for position, value in enumerate(dataset.Month)]
@randyzwitch
randyzwitch / wordpress-seaborn.py
Last active August 29, 2015 14:04
Re-creating WordPress chart
import seaborn as sns
import matplotlib.pyplot as plt
#Set theme and size, remove axis titles
sns.set_style("whitegrid")
sns.set_context({"figure.figsize": (17, 6)}) #Passing in matplotlib commands directly
#Plot data
#Use dataset.index as my x_order, to ensure proper ordering by using numeric series
sns_views = sns.barplot(dataset.Month, "Views", data = dataset, color = "#278DBC", dropna = False, ci=None, x_order=dataset.Month)
@randyzwitch
randyzwitch / wordpress-julia-gadfly.jl
Created July 17, 2014 19:47
Re-creating WordPress chart using Gadfly
using Gadfly, DataFrames
set_default_plot_size(24cm, 8.5cm)
#Read in data using DataFrames package
df = readtable("visits_visitors.csv")
#Fill :Visitors with 0 to replace NA
df[:Visitors] = array(df[:Visitors], 0)
plot(df,
@randyzwitch
randyzwitch / wordpress-julia-plotly.jl
Created July 21, 2014 00:43
Re-creating WordPress chart using Plot.ly
using Plotly, DataFrames
Plotly.signin("username", "api-key")
#Read in data
df = readtable("visits_visitors.csv")
#Create page views plot
views = [
["x" => df[:Month], "y" => df[:Views], "type" => "bar", "name" => "Views", "marker" => ["color" => "rgb(39, 141, 188)"]]
]