Skip to content

Instantly share code, notes, and snippets.

@mwfrost
mwfrost / MSHA.r
Created December 22, 2010 17:55
MSHA Mine Data from data.gov
# data from data.gov
# url title
# http://www.data.gov/details/4055 Accident Injuries Data Set
# http://www.data.gov/details/4134 Employment/Production Data Set - Yearly (Coal only)
# http://www.data.gov/details/4135 Employment/Production Data Set - Quarterly (Coal only)
# http://www.data.gov/details/4136 Mines Data Set
# http://www.data.gov/details/4137 Mine Address of Record Data Set
# http://www.data.gov/details/4138 Mine Inspections
# http://www.data.gov/details/4139 Mine Inspections
@mwfrost
mwfrost / percentify.r
Created January 17, 2011 19:03
convert to percentage format
percentify <- function(x, r=1, keep.zeros=FALSE, na.code=""){
ifelse (as.numeric(x) != 0 | keep.zeros==TRUE, paste( format(round ( as.numeric(x) * 100, r) , nsmall=r) , '%', sep='')
,
na.code)
}
# usage example:
sapply(dataframe[3:5], percentify, 1)
@mwfrost
mwfrost / ExcelODBC.r
Created January 18, 2011 18:21
Open and query an Excel workbook in R
channel <- odbcConnectExcel('../file name.xls', readOnly = TRUE)
dat <- sqlQuery(channel, 'SELECT * FROM [workbook name$]', stringsAsFactors=FALSE)
@mwfrost
mwfrost / pandoc_convert.py
Created January 19, 2011 19:32
Process a markdown file with pandoc
os.system('pandoc -f markdown -t html cair_3.markdown -o out.html -S')
Can be run as a SublimeText console command
@mwfrost
mwfrost / ListXMLNodes
Created February 8, 2011 15:09
Use XPath 2.0 syntax to list all the nodes in an XML file
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="text()"/>
<xsl:template match="*|@*">
<xsl:value-of select="
string-join(
distinct-values(
(//*|//@*)
/string-join(
(ancestor::node()/name(),
if (self::attribute())
@mwfrost
mwfrost / po.xsd
Created February 9, 2011 22:01
Sample schema
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:annotation>
<xsd:documentation xml:lang="en">
Purchase order schema for Example.com.
Copyright 2000 Example.com. All rights reserved.
</xsd:documentation>
</xsd:annotation>
<xsd:element name="comment" type="xsd:string">
<xsd:annotation>
<xsd:documentation>doc for comment</xsd:documentation>
@mwfrost
mwfrost / ListXSDNodes.xslt
Created February 9, 2011 22:01
Minimal parser, only returns name()
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>
<xsl:template match="*|@*">
<xsl:value-of select="
string-join(
distinct-values(
(//*|//@*)
/string-join(
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.exchangenetwork.net/schema/ghg/1" >
<xsl:template match="*|@*">
<xsl:value-of select="
string-join(
(//*|//@*)
/string-join(
(ancestor::node()/
(
@mwfrost
mwfrost / column_apply.r
Created April 26, 2011 19:00
apply table() to every column
apply(dat, 2, table)
@mwfrost
mwfrost / Markdown.sublime-build
Created May 5, 2011 18:33 — forked from dvhthomas/Markdown.sublime-build
Pandoc powered Sublime Text 2 build provider for Markdown files: HTML the easy way
{
"cmd": ["pandoc.exe", "--to=html", "--output=$file.html", "$file"],
"selector": "source.md"
}