Skip to content

Instantly share code, notes, and snippets.

View martinctc's full-sized avatar
🔥
Packaging up my workflows

Martin Chan martinctc

🔥
Packaging up my workflows
View GitHub Profile
@martinctc
martinctc / normalise-replace.vbs
Last active May 16, 2017 14:35
Normalise selected range of data - REPLACES EXISTING DATA Using min-max scaling
Sub normalise()
Dim rng, cel As Range
Set rng = Application.InputBox("Please select source range area (exclude headers)", "Source data", Default:="=$A$3:$G$10", Type:=8)
Dim max, min As Long
max = Application.max(rng)
min = Application.min(rng)
Debug.Print max
Debug.Print min
@martinctc
martinctc / CombineRowsFromSheets.vbs
Last active May 16, 2017 14:55
If you have a specific number of worksheets that have the exact same columns - This macro combines the rows into a new created sheet. The effect is similar to rbind() in R.
Sub Combine()
Dim Acc_Name As Variant
Dim i As Long
Dim j As Integer
Dim newsheet As Worksheet
On Error GoTo Errorcatch
Acc_Name = Array("CLloyds", "NFQ", "SSaver", "CISA", "CLMSaver")
@martinctc
martinctc / complete case.r
Last active May 19, 2017 16:48
Missing Values Exercise - Read in data from XLSX file, using the readxl package - Combine two data sets using merge(), referencing with a key - complete.cases() and filter() to create a data frame without missing values
#readClipboard()
#Read copied folder path as something that R can read in properly
library(readxl)
library(tidyverse)
library(stringr)
library(forcats)
setwd("C:\\Users\\Martin.Chan\\Desktop\\R workdesk\\missing values")
add.dt<-read_excel("ORD-272769-F4S6 - Additional info for UK.xlsx",sheet="Additional info",na="NA")
@martinctc
martinctc / Format a chart.vbs
Created June 14, 2017 11:01
Define specifications for a Chart object and apply to selected Chart
Sub Format_a_Chart()
'r converts cm to points
r = 28.3464567
Dim shp1 As Shape
Dim i, j, k As Long
Set shp1 = ActiveWindow.Selection.ShapeRange(1)
@martinctc
martinctc / String Replace nth.r
Created August 26, 2017 21:54
Replace nth occurring value in a string
str_replace_n <- function(x, pattern, replace, n){
g <- gregexpr(pattern,x)[[1]][n]
output <- paste0(substr(x,1,g-1),replace,substr(x,g+1,nchar(x)))
output
}
str_replace_n("ALLAHABAD","A","U",3)
#[1] "ALLAHUBAD"
str_replace_nth <- function(x, pattern, replacement, n) {
@martinctc
martinctc / 3-Table Tinkerer.vbs
Created October 2, 2017 15:25
Table tinkerer tool created for three tables iterated across many slides
Sub table_tinkerer_threetabs()
'Table tinkerer tool created for three tables iterated across many slides
'Alt+F11 to start up code module
'Insert New Module >>
'Select Table >> Run
@martinctc
martinctc / Tweak Scatter Labels for PPT.vbs
Created October 27, 2017 16:39
Tweak Labels for Scatterplots in PowerPoint
Sub ScatterLabelsTweak()
Dim sld As Slide
Dim shp As Shape
Dim sr As Series
Dim chrt As Chart
Dim i, j, k, m As Long
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
@martinctc
martinctc / match_counter.R
Last active November 10, 2017 12:36
[Match Counter (by target column)] Function returning a data frame summarising the counts and percentages of row-by-row identical values across the columns of a data frame. Pass a data frame containing some columns including a "target" column - the "target" column is the main column that you want to compare with other columns. Pass the arguments…
match_counter <- function(df,target){
arguments <- as.list(match.call())
targ <- deparse(substitute(target)) #variable to string
#Produce a data frame of all columns on whether they match values of target column
df %>%
mutate_all(funs(.==eval(parse(text=targ)))) %>%
colSums(na.rm = TRUE) %>% data.frame() %>%
rownames_to_column() -> matches
@martinctc
martinctc / Time Stamp.vbs
Created November 11, 2017 22:37
[Time stamp in Excel VBA] Create time stamp string in Excel VBA #Excel VBA
Sub TimeStamp()
time_stamp = Format(DateTime.Now, "yyyyMMdd_hhmmss")
Debug.Print time_stamp
End Sub
@martinctc
martinctc / Change Data Type.R
Last active November 13, 2017 14:41
[Change data types in bulk] Change data types in bulk using dplyr as.numeric() as.character() as.factor() #R
Q12 <- data.frame(num=1:2000,type=sample(c("Sandwich","Porridge","Muesli/cereal","Other","No breakfast"),2000,
replace=TRUE,prob=c(0.2,0.15,0.25,0.15,.25)))
glimpse(Q12)
#All columns as character
Q12 %>%
mutate_all(funs(as.character)) %>%
glimpse()