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 / analyse_ngrams.R
Created February 13, 2020 16:22
[ngram analyser] #R
#### Analyse ngrams ####
ngram_analyser <- function(text,count_col, n = 2){
tibble(text = text, line = length(text)) -> ori_tb
ori_tb %>%
tidytext::unnest_tokens(output = phrase, input = text, token = "ngrams", n = n) %>%
count(phrase, sort = TRUE) %>%
rename(!!sym(count_col):="n") -> ngram_df
return(ngram_df)
@martinctc
martinctc / AnalysisTableFormatter.vbs
Created January 27, 2020 17:42
[Analysis Table Formatter] #VBA #Excel Loop through all the sheets and format outputs
Sub AnalysisTableFormatter()
Dim WS As Excel.Worksheet
Dim row_n As Long
CurrentWorkbook = ThisWorkbook.FullName
For Each WS In ThisWorkbook.Worksheets
WS.Activate
Columns("A:A").ColumnWidth = 16
Columns("B:B").ColumnWidth = 18
@martinctc
martinctc / mt_analyse.R
Created January 20, 2020 17:31
[Music Testing Analysis] #R
mt_analyse <- function(df,sample,weight){
df %>%
group_by(Week_Date) %>%
filter(Sample==sample) %>%
mutate_at(vars(X7_Days,X28_Days),~./sum(.)) %>% # Calculate weights
mutate_at(vars(Score,Fam_Score,Familiar,Per_Love,Per_Pos,Per_Hate,Per_Burn),list(~.*.data[[weight]])) %>%
summarise_at(vars(Score,Fam_Score,Familiar,Per_Love,Per_Pos,Per_Hate,Per_Burn),~sum(.))
}
cell_adder <- function(df1, df2, key){
@martinctc
martinctc / tinsmen_card_picker.R
Created December 13, 2019 14:10
[Tinsmen Card Picker] #R
library(tidyverse)
tinsmen <- c("Andis",
"Sterer",
"Cliff",
"Ken",
"Ocean",
"Newton",
"Brandon",
"Jaf",
"K Ko",
@martinctc
martinctc / slicematch.R
Created December 9, 2019 17:41
[Manually reorder rows using dplyr] #R
library(tidyverse)
tibble(c1 = c("B", "A", "C"),
c2 = c(2, 4, 3)) -> tb
tb %>%
slice(match(c1, c("C", "B", "A")))
@martinctc
martinctc / find_and_replace.txt
Last active October 25, 2019 12:28
[Find and replace characters in file name using PowerShell] #PowerShell
Get-ChildItem *.jpg | Rename-Item -NewName { $_.Name -replace '\(','_' }
Get-ChildItem *.jpg | Rename-Item -NewName { $_.Name -replace ' ','' }
Get-ChildItem *.jpg | Rename-Item -NewName { $_.Name -replace '\)','' }
# Another example
Get-ChildItem *.Rmd | Rename-Item -NewName { $_.Name -replace 'rajar','digit' }
@martinctc
martinctc / rowwise_ranks.R
Created August 26, 2019 20:57
[Create n number of columns returning the row-wise ranks of values in selected columns] #R
## Create n number of columns returning the row-wise ranks of values in selected columns
tibble(a = c(200, 300, 100, 150),
b = c(400, 120, 600, 700),
c = c(500, 100, 200, 300)) %>%
mutate(ranks = select(., a, b, c) %>%
apply(1, function(x){
x %>%
order() %>%
paste(collapse = ";")
@martinctc
martinctc / candle_stick_plotly.R
Created August 22, 2019 09:13
[Candle Stick Chart in Plotly Example] #R
library(plotly)
library(quantmod)
getSymbols("AAPL",src='yahoo')
# basic example of ohlc charts
df <- data.frame(Date=index(AAPL),coredata(AAPL))
df <- tail(df, 30)
p <- df %>%
@martinctc
martinctc / counts_table_per_column.R
Created August 20, 2019 16:51
[Create a counts table for each column using map] #R
library(tidyverse)
tibble(x1 = c("a", "b", "c"),
x2 = c("v", "x", "y"),
x3 = c("w", "u", "u")) %>%
map(~as_tibble(table(.)))
@martinctc
martinctc / ping_batch_logger.txt
Created August 20, 2019 15:24
[Ping Batch Logger] Created 12 Nov 2018 #CommandLine
@echo off
set /p host=Please enter host address:
set logfile=Log_%host%.log
echo Target Host = %host% >%logfile%
for /f "tokens=*" %%A in ('ping %host% -n 1 ') do (echo %%A>>%logfile% && GOTO Ping)
:Ping
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ') do (
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%