Skip to content

Instantly share code, notes, and snippets.

Avatar
🔥
Packaging up my workflows

Martin Chan martinctc

🔥
Packaging up my workflows
View GitHub Profile
@martinctc
martinctc / rank_by_group.R
Last active Nov 1, 2021
[Rank a data frame with a grouping variable using entirely base R] #R
View rank_by_group.R
#' @title
#' Rank a data frame by grouping variable using base R
#'
#' @description
#' This function ranks a specified column in a data frame by group using entirely base R functions.
#' The underlying function is `rank()`, where additional arguments can be passed with `...`.
#' The grouping variable is specified as a string using the argument `group_var`, and the variable to rank is
#' specified using the argument `rank_var`. The operation is analogous to using `group_by()` followed by
#' `mutate()` in {dplyr}.
#' See example below using the base dataset `iris`.
@martinctc
martinctc / repeat rows based on n
Created Jul 13, 2021
[Duplicate rows in data frame based on n] #R
View repeat rows based on n
# multiply values based on weights
wtest <-
data.frame(
x = c("cats", "dogs", "birds", "cats"),
y = c(1, 2, 3, 2)
)
wtest[rep(seq_len(nrow(wtest)), wtest$y),]
@martinctc
martinctc / FilterTwoSeriesCopyAndPaste.vbs
Last active Jul 2, 2021
FilterTwoSeriesCopyAndPaste
View FilterTwoSeriesCopyAndPaste.vbs
Sub FilterTwoSeriesCopyAndPaste()
Dim S1_String, S2_String As String
'READ VALUES FROM SETTING SHEET -------------------------------------------------------
S1_String = Worksheets("Settings").Range("B2").Value
S2_String = Worksheets("Settings").Range("B3").Value
Debug.Print S1_String
@martinctc
martinctc / map on Rd files.R
Last active Apr 2, 2021
[Loop through Rd files] #R
View map on Rd files.R
# Load packages -----------------------------------------------------------
library(tidyverse)
# Path where all Rd files live --------------------------------------------
man_path <-
here::here("man")
@martinctc
martinctc / information value.R
Last active Mar 7, 2021
[Create base version of Information Value calculations] #R
View information value.R
## Implement Information Value in base?
iris_iv <-
iris %>%
mutate(IsSetosa = ifelse(Species == "setosa", 1, 0))
## Information
infoTables <-
Information::create_infotables(
data = iris_iv,
@martinctc
martinctc / Developer functions in R
Last active Apr 14, 2021
[List of R developer tools] #R
View Developer functions in R
# Regular R CMD check
devtools::check()
# Check R-hub
devtools::check_rhub()
# Cross-platform R CRAN check
rhub::check_for_cran()
# Return a character vector with the names of dependencies - CRAN only
@martinctc
martinctc / date expansion.R
Created Feb 23, 2021
[Date Expansion using Sundays] #R
View date expansion.R
## Date expansion
validweeks <-
unique(clean_wbq_totalview$Date) %>%
purrr::map(function(x){
seq(x, x + 6, by = "days")
}) %>%
purrr::reduce(c)
@martinctc
martinctc / import_to_fst.R
Last active Feb 10, 2021
[Import to FST] #R
View import_to_fst.R
import_to_fst <- function(path){
temp_df <- import_wpa(path)
path_fst <- gsub(pattern = "csv$",
replacement = "fst",
x = path)
fst::write_fst(temp_df, path_fst)
@martinctc
martinctc / jittered_ordinal_points.R
Created Jan 3, 2021
[Visualize ordinal data over time with jittered points] #R
View jittered_ordinal_points.R
library(tidyverse)
10:30 %>%
map(function(x){
prob_v <-
c(x,
100 - 2*x,
100 - x - (100 - 2*x))
@martinctc
martinctc / geom jitter labels.R
Created Oct 20, 2020
[geom label or text to align with geom_jitter()] #R
View geom jitter labels.R
library(tidyverse)
library(hkdatasets)
hk_accidents %>% glimpse()
## These charts look ridiculous but at least the jitter points are aligned with the text
hk_accidents %>%
filter(Year == 2017) %>%
ggplot(aes(x = Natural_Light , y = No__of_Vehicles_Involved)) +