Skip to content

Instantly share code, notes, and snippets.

View TimTeaFan's full-sized avatar

Tim Tiefenbach TimTeaFan

View GitHub Profile
@TimTeaFan
TimTeaFan / monty_hall.R
Created January 24, 2024 10:59
Monty Hall Problem
reveal_door <- function(doors) {
if ("car" %in% names(doors)) {
return(doors[names(doors) != "car"])
}
sample(doors, size = 1)
}
play_monty_hall <- function(switch_doors = FALSE) {
@TimTeaFan
TimTeaFan / many_dv_models.R
Created May 30, 2023 09:20
Tidyverse many models using several dependent variables
``` r
library(tidyverse)
indep_vars <- c("vs", "am", "gear", "carb")
dep_vars <- c("mpg", "disp", "hp")
init_grid <- tibble(dep_var = dep_vars,
indep_vars = list(indep_vars))
@TimTeaFan
TimTeaFan / nested_data_table.R
Created March 24, 2023 20:13
using `table()` in list-column of nested data
library(dplyr)
mtcars %>%
nest_by(gear) %>%
mutate(freq = rlang::list2(
"gear_{gear}_carb" := table(data$carb)
)) %>%
pull(freq)
#> $gear_3_carb
@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
@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 / 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 / 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 / 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_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 / 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))