Skip to content

Instantly share code, notes, and snippets.

View erikerhardt's full-sized avatar

Erik Erhardt erikerhardt

View GitHub Profile
@erikerhardt
erikerhardt / AbbreviateCaptions Word Macro.docm
Last active January 20, 2021 20:46
MS Word Abbreviate existing Figure and Table cross-references
## Abbreviate existing Figure and Table cross-references
## https://www.msofficeforums.com/word/11965-making-cross-reference-say-fig-instead-figure.html
# Add with View > Macros > View Macros > Create > Select the current file (Project .. ThisDocument)
# > Paste this Macro > save document in docm format
Sub AbbreviateCaptions()
Dim Fld As Field
For Each Fld In ActiveDocument.Fields
With Fld
If Left(.Result.Text, 6) = "Figure" Then
## Convert Excel Dates
# Differences between Windows and Mac Serial number date systems
# https://support.microsoft.com/en-us/office/date-systems-in-excel-e7fe7167-48a9-4b96-bb53-5612a800b487#ID0EAACAAA=Windows
# Excel supports two date systems, the 1900 date system and the 1904 date system.
# Each date system uses a unique starting date from which all other workbook dates are calculated.
# All versions of Excel for Windows calculate dates based on the 1900 date system.
# Excel 2008 for Mac and earlier Excel for Mac versions calculate dates based on the 1904 date system.
# Excel 2016 for Mac and Excel for Mac 2011 use the 1900 date system, which guarantees date compatibility with Excel for Windows.
# For example, July 5, 2011, can have two different serial numbers, as follows:
@erikerhardt
erikerhardt / key_variables_completion.R
Last active March 28, 2021 22:59
R Key variables completion, Efficient completion of keys from multiple data sources
# key_variables_completion.Rmd at https://statacumen.com/2021/03/28/r-key-variables-completion/
library(tidyverse)
# Combine rows with different data values
f_coalesce_by_column <-
function(
dat
) {
# for matching group_by() keys, keeps first non-NA value for each column
```{r codebook}
# http://sandsynligvis.dk/articles/18/codebook.html
# https://github.com/ekstroem/dataMaid
#library(dataMaid)
#dataMaid::makeCodebook(dat_PreScr, replace = TRUE, openResult = FALSE, codebook = TRUE)
#dataMaid::makeCodebook(dat_Elig , replace = TRUE, openResult = FALSE, codebook = TRUE)
#dataMaid::makeCodebook(dat_pdp , replace = TRUE, openResult = FALSE, codebook = TRUE)
# Author's blog posts on using
# https://sandsynligvis.dk/2017/08/21/datamaid-your-personal-assistant-for-cleaning-up-the-data-cleaning-process/
@erikerhardt
erikerhardt / Excel replacetexts
Last active July 25, 2021 20:30
MS Excel BVA to replace a list of text strings with another set of strings
# =Lookup() is for whole cells https://support.microsoft.com/en-us/office/lookup-function-446d94af-663b-451d-8251-369d5e3864cb
# below is to replace a text string inside a longer string
# Original code: https://www.howtoexcel.org/vba/how-to-find-and-replace-multiple-text-strings-within-a-text-string/
# How to use: https://www.howtoexcel.org/vba/how-to-use-the-vba-code-you-find-online/
# Step 1: Enable the Developer tab in the Ribbon.
# Go to the File tab.
# Click Options section.
# Click the Customize Ribbon section.
# don't print readr::read_csv() column types
options(readr.num_columns = 0)
options(readr.show_col_types = FALSE)
# remove attributes after readr::read_csv()
# attr(df, "spec") <- NULL
# remove $ and , from dollar amounts; (5) is negative 5, so remove ")" and replace "(" with "-"
`Demand Sales` = gsub("[(]", "-", gsub("[\\$,)]", "", `Demand Sales` )) %>% as.numeric()
@erikerhardt
erikerhardt / Rmd_chunk_eval_T_F
Last active March 28, 2022 00:47
Rmd chuck to turn on/off eval for future code chunks
```{r, eval = TRUE}
# XXX-xxx
######################################## ########################################
####### STOP EVAL ######## ####### STOP EVAL ########
######################################## ########################################
knitr::opts_chunk$set(eval = FALSE) # TRUE
######################################## ########################################
####### STOP EVAL ######## ####### STOP EVAL ########
######################################## ########################################
@erikerhardt
erikerhardt / e_table1_summaries
Last active May 24, 2022 03:04
moonBook tables
```{r, results='asis'}
# list of column variables for table
list_var_col_names <- "Group"
list_var_col_labels <- "Group"
# list of row variables for table
list_var_row_names <- c(Var[["list"]][["var_resp"]] , Var[["list"]][["var_covar"]] )
list_var_row_labels <- c(Var[["label"]][["var_resp"]], Var[["label"]][["var_covar"]])
fn_root <- "./out/tab1_vars_dx_demo"
@erikerhardt
erikerhardt / read_fn_current.R
Created September 5, 2022 14:38
Read current (most recent) files in folder
## Read current files
# Assumed file naming structure is Filename_YYYYMMDD.csv
# list of all files
fn_list_data_raw <- dir("data/")
# filenames to match
fn_dat_list_in <-
c(
@erikerhardt
erikerhardt / knitr_helpers.R
Created October 25, 2022 23:27
knitr helpers
# from inline to yaml chunk options
knitr::convert_chunk_header()
# Verbatim engine for instruction
# https://bookdown.org/yihui/rmarkdown-cookbook/verbatim-code-chunks.html