Skip to content

Instantly share code, notes, and snippets.

View erikerhardt's full-sized avatar

Erik Erhardt erikerhardt

View GitHub Profile
@erikerhardt
erikerhardt / convert_character_to_factor.R
Last active March 28, 2024 19:23
Convert all character to factor
iris_e <- iris
str(iris_e)
# from factor to character
iris_e <- iris_e %>% dplyr::mutate(tidyselect::across(tidyselect::where(is.factor), as.character))
str(iris_e)
# from character to factor
iris_e <- iris_e %>% dplyr::mutate(tidyselect::across(tidyselect::where(is.character), as.factor))
str(iris_e)
# remove all-NA columns
@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"
[^\x00-\x7F]
To fix this, you have to find the Unicode and replace it with its ASCII equivalent. To do this: Ctrl-F to find, search for “[^\x00-\x7F]” (without quotes), selectRegexfor regular expressions, and find theNextone.
# Replace unicode in all variables
dat_all <-
dat_all |>
dplyr::mutate(
dplyr::across(
```{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/
# 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 / 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.
@erikerhardt
erikerhardt / f_ggplot_grid_workflow.R
Last active March 12, 2024 15:35
R ggplot plot and grid.arrange workflow
## Erik's workflow template for plots in R ggplot2
library(erikmisc) # https://github.com/erikerhardt/erikmisc/
# https://patchwork.data-imaginist.com/articles/guides/layout.html
# When lazy evaluation (nonstandard evaluation NSE) is an issue with plots into a list
# force evaluation by running cowplot (have not found a better way using force() or eval() or tidy_eval())
# p_list[[ i_plot ]] <- p |> cowplot::plot_grid()
@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
## 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: