Skip to content

Instantly share code, notes, and snippets.

View TimTeaFan's full-sized avatar

Tim Tiefenbach TimTeaFan

View GitHub Profile
@TimTeaFan
TimTeaFan / head_count.R
Created July 3, 2020 13:33
Calculate Head Count over Month based on start / end date.
library(tidyverse)
library(lubridate)
dat <- tribble(~id, ~start, ~end,
0102, "2018-01-02", "2018-01-07",
2190, "2018-01-01", "2018-03-06",
2432, "2018-01-03", "2018-05-07",
0121, "2018-01-03", "2018-02-04",
0121, "2018-01-02", "2018-01-15",
)
@TimTeaFan
TimTeaFan / shiny_overlay.R
Created September 18, 2020 21:30
Shiny Overlay Page
# Adaption of w3schools overlay page
# https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_overlay_text
library(shiny)
library(shinyjs)
# Overlay js function
jsCode <- "
shinyjs.overlay_on = function() {
@TimTeaFan
TimTeaFan / d.R
Created April 18, 2021 11:05
combine and keep attributes of same length
l1 <- structure(list(x = 1), attr = 1)
l2 <- structure(list(y = 2), attr = 2)
d <- function(...) {
dots <- match.call(expand.dots = FALSE)$...
attr_ls <- lapply(dots, function(x) attributes(get(as.character(x))))
attr_ls <- lapply(purrr::transpose(attr_ls), unlist)
out <- c(...)
@TimTeaFan
TimTeaFan / select_where.R
Created April 22, 2021 17:25
use where in select and across with custom function
library(tidyverse)
contains_comma <- function(x) {
if (is.character(x) || is.factor(x)) {
any(str_detect(x, ","))
} else {FALSE}
}
gss_cat %>%
select(where(contains_comma))
@TimTeaFan
TimTeaFan / dplyr_subtotals.R
Last active June 14, 2021 22:06
Totals and subtotals with dplyr and bind_rows
library(dplyr)
set.seed(123)
dat <- tibble(main_grp = rep(c("a", "b"), each = 4),
sub_grp = rep(c("x", "y"), 4),
value = rnorm(8))
dat %>%
bind_rows(., mutate(dat, sub_grp = "subtotal")) %>%
@TimTeaFan
TimTeaFan / seq_sum.R
Last active June 20, 2021 13:20
seq_sum() function
seq_sum <- function(from, to, .sum, exact = FALSE) {
a <- from + to
m <- a/2
d <- .sum - a
if (exact && (d %% m !=0)) {
stop("sequence didn't converge.")
}
i <- (d / m) + 2
@TimTeaFan
TimTeaFan / dplyr_add_rows.R
Created August 8, 2021 20:43
Add rows with total and group means in dplyr
library(dplyr)
# Add Total Mean for numeric variables
iris %>%
add_row(
summarise(.,
Species = "Total mean",
across(where(is.numeric), mean))
) %>%
tail() # for printing
@TimTeaFan
TimTeaFan / while_as_reduce
Last active August 3, 2022 22:09
whileループをむりやりにreduceで書き換えた(あまりお勧めしない)
# this reduce() and done() combo mimics a while loop
# coming from this tweet
# https://twitter.com/norimitsunishi1/status/1554353454360932353?s=21&t=lopKMBKt-SKQfOlrM5B_Iw
# of course this is NOT a good solution, since the input to reduce has a limited length, ...
# ... whereas the while loop will just go on forever until it breaks.
library(purrr)
f <- function(char, char2) {
@TimTeaFan
TimTeaFan / many_models_advanced.R
Last active October 1, 2022 19:43
many model approach with multi-level data and models
library(tidyverse)
library(gapminder)
# lists of independent variables
indep_var_country <- list(
"gdp_only" = "gdpPercap",
"gdp_and_pop" = c("pop", "gdpPercap")
)
indep_var_continent <- append(
@TimTeaFan
TimTeaFan / gist:6324c446f5f95505ff86a014a33ae1c0
Created January 30, 2023 17:22
dplyr_workflow_compare_many_glm_models
library(tidyverse)
set.seed(123)
# some random dataa
my_dat <- tibble(
y = rbinom(300, 1, 0.5), # outcome 1
z = rbinom(300, 1, 0.5), # outcome 2
t = rbinom(300, 1, 0.5), # outcome 3
indep_var = runif(300), # this is our independet variable