Skip to content

Instantly share code, notes, and snippets.

View mattjcamp's full-sized avatar
😍
Analyzing

Matt Campbell mattjcamp

😍
Analyzing
View GitHub Profile
@mattjcamp
mattjcamp / sqlite_import.md
Last active April 4, 2016 12:40
SQLite Import from command line

Step One: Copy Everything

  • Copy SQLite.exe into the directory
  • Copy database into the directory
  • Copy source data file into the directory

NOTE Can also just reference paths if preferred

Step Two: Open Database

@mattjcamp
mattjcamp / Timing Data.r
Created December 30, 2015 11:41
R RStudio Timing for R code execution
get.timing.data <- function(task, run_order){
start.time <- Sys.time()
time <- system.time({CODE TO EXECUTE HERE})
df <- data.frame(list(Task = task,
Run_Order = run_order,
Elapsed = time[3]))
return(df)
@mattjcamp
mattjcamp / Apply Function to Vector.r
Created December 29, 2015 16:18
R RStudio Apply function to vector sapply
f$Sector <- sapply(f$Sector,
FUN = data.vacuum.adapt,
category = "Sector")
@mattjcamp
mattjcamp / Frequency-Distribution.r
Last active September 22, 2023 12:59
R RStudio Frequency Distribution
# s$SCORE is a vector
freq <- cbind(
Freq = table(s$SCORE),
Cumm_Freq = cumsum(table(s$SCORE)),
Perc = round(prop.table(table(s$SCORE)) * 100, 4),
Cumm_Perc = round(cumsum(prop.table(table(s$SCORE))) * 100, 4)
)
freq <- as.data.frame(freq)
@mattjcamp
mattjcamp / Load CSV into Table.js
Last active September 22, 2023 13:48
HTML Load CSV into HTML Table
d3.csv("aggregated_total.csv", function(d) {
var table = d3.select('#CSV_Table')
.append('table')
.attr("class", "center");
var trh = table.append('tr');
trh.append('td')
.text("Year")
@mattjcamp
mattjcamp / Standard Deviation Population.r
Last active September 22, 2023 13:53
R RStudio Standard deviation for a population (not sample)
# Returns the standard deviation for a
# population (using N and not N-1 as
# denominator)
sdp <- function(x){
# I adapted this from an answer I found
# on Stack Overflow
# Reference
@mattjcamp
mattjcamp / CLI-Use-XLST
Last active September 22, 2023 13:39
XML HTML XLST Transform XML to HTML
# Reference
https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man1/xsltproc.1.html">Apple document on xsltproc
# submit the line below to the Mac terminal
xsltproc index.xsl index.xml > output.txt
@mattjcamp
mattjcamp / Regular Expressions.r
Last active September 22, 2023 13:54
R RStudio Regex to test for state abbreviation
if(!grepl("^[A-Z]{2}$", location))
stop(sprintf("***%s*** is not a valid value for input parameter location.
Choose the two letter state abbreviation", location))
# Replace
# Replaces anything with _#### with %i in a data frame
temp$TABLE_NAME <- sub("_[1,2][0-9][0-9][0-9]", "_%i", temp$TABLE_NAME)
# Look at records that fit a pattern
@mattjcamp
mattjcamp / transpose-pivot.sql
Last active September 22, 2023 12:50
T-SQL Transpose Pivot
/*Make Table Wide, Rows to Columns*/
SELECT Cohort, State,
MAX(CASE WHEN GENDER = 'F' THEN N ELSE NULL END) AS N_F,
MAX(CASE WHEN GENDER = 'M' THEN N ELSE NULL END) AS N_M,
MAX(CASE WHEN GENDER = 'F' THEN SAT_Verbal ELSE NULL END) AS SAT_Verbal_F,
MAX(CASE WHEN GENDER = 'M' THEN SAT_Verbal ELSE NULL END) AS SAT_Verbal_M,
MAX(CASE WHEN GENDER = 'F' THEN SAT_Math ELSE NULL END) AS F_SAT_Math_F,
MAX(CASE WHEN GENDER = 'M' THEN SAT_Math ELSE NULL END) AS M_SAT_Math_M,
MAX(CASE WHEN GENDER = 'F' THEN SAT_Writing ELSE NULL END) AS SAT_Writing_F,
@mattjcamp
mattjcamp / mine-workbook.vbs
Created April 2, 2015 12:38
VBA Mine an Excel workbook
'Macro to help mine data from an Excel workbook
'This reads through each worksheet in a workbook and outputs each value into
'a destination sheet. For each row it reads until it reaches a blank cell (across columns).
'For each sheet, it reads until it reaches a blank cell (down rows)
Sub MineDataFromWorkbook()
Dim wb As Workbook
'Replace NAME with workbook name (must be open in Excel)
Set wb = Application.Workbooks("NAME")