Skip to content

Instantly share code, notes, and snippets.

View stephlocke's full-sized avatar

Steph Locke stephlocke

View GitHub Profile
@stephlocke
stephlocke / FunctionComparison
Created September 10, 2014 09:16
Provides indicative view of what I'm trying to achieve by changing functions and testing the before and after results
# Example functions that would be in seperate files in the R directory of the package
simpleFn1 <- function(a){
myConst <- 10
a*myConst
}
simpleFn2 <- function(a){
myConst <- 5
a/myConst
}
@stephlocke
stephlocke / C++rate
Created September 17, 2014 18:58
internally used rate function from optiRum::RATE written in C++
library(Rcpp)
cppFunction('double rate(double nper, double pmt, double pv){
float rate1 = 0.01 ;
float rate2 = 0.05 ;
float newrate = 0 ;
int n = 10;
for (int i = 0; i < n; ++i) {
double pv1 = floor((-pmt/rate1 * (1 - 1/pow((1 + rate1),nper)))*100+0.5)/100 - pv ;
double pv2 = floor((-pmt/rate2 * (1 - 1/pow((1 + rate2),nper)))*100+0.5)/100 - pv ;
if (pv1 != pv2) {
@stephlocke
stephlocke / postcodetrim
Last active August 29, 2015 14:09
Overcoming file limitations and frustrations of using Excel & data import wizard
# Source of origpostcodedata data: https://geoportal.statistics.gov.uk/geoportal/catalog/search/resource/details.page?uuid=%7BD14C75CE-95C6-4E9F-8753-ECDD88BC2B7D%7D
# Source of postcodefiles data:https://www.ordnancesurvey.co.uk/opendatadownload/products.html
library(foreach)
library(data.table)
setwd("~")
postcodefiles<-dir("../Downloads/codepo_gb/Data//CSV/",full.names = TRUE)
# Slim load of 2.5m records into memory with f(ast)Read function
origpostcodedata<-fread("../Downloads/ONSPD_MAY_2013_csv/Data//ONSPD_MAY_2013_UK_O.csv",
select=c("pcd","pcd2","pcds","dointr",
@stephlocke
stephlocke / ExeterCode.R
Last active August 29, 2015 14:13
SQLSaturday Exeter Submissions
# This script will work for any SQL Saturday where schedule has not been confirmed (not tested on others!) just change the URL
library(rvest)
library(data.table)
library(ggplot2)
#wordcloud only required packages
library(wordcloud)
library(tm)
library(foreach)
@stephlocke
stephlocke / DNRvba
Created January 13, 2015 10:32
Dynamic named range generator
#Const LateBind = True
Function RegExpSubstitute(ReplaceIn, _
ReplaceWhat As String, ReplaceWith As String)
#If Not LateBind Then
Dim RE As RegExp
Set RE = New RegExp
#Else
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
#End If
@stephlocke
stephlocke / iris
Created January 15, 2015 14:25
Iris transformation file
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Iris</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Sepal.Length</th>
@stephlocke
stephlocke / abstract
Last active August 29, 2015 14:13
My abstract template
Title (max 60 characters)
------
Short description (Quick sell! max 135 characters)
------
Medium description (Sell & outline some content. max 100 words)
------
@stephlocke
stephlocke / 1st method
Last active August 29, 2015 14:14
Exploring program flow with magrittr
library(data.table)
orig_subset<-function(workingdata){
workingdata[["inputs"]][Species!='virginica']
}
orig_mult<-function(workingdata){
workingdata[["subset"]][,.SD*1.1,by=Species]
}
@stephlocke
stephlocke / bloblogs
Last active August 29, 2015 14:14
Parsing Azure Blob logs
library(data.table)
library(chron)
library(ggplot2)
library(stringr)
# This doesn't cope with spaces in URLs grr!!
url_pattern <- "http[s]?://(?:[@blank:]|[a-zA-Z]|[0-9]|[$-_@.&+ ]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+"
## Read data in from a text file
raw.log <- data.table(scan("bloblog.txt", character(0), sep = "\n"))
@stephlocke
stephlocke / Renviron.site
Created February 7, 2015 18:26
For circumventing R home user directory issues
R_USER = "E:/Dropbox/hobbies/R"