This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ``` 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(dplyr) | |
| mtcars %>% | |
| nest_by(gear) %>% | |
| mutate(freq = rlang::list2( | |
| "gear_{gear}_carb" := table(data$carb) | |
| )) %>% | |
| pull(freq) | |
| #> $gear_3_carb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) { | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(dplyr) | |
| # Add Total Mean for numeric variables | |
| iris %>% | |
| add_row( | |
| summarise(., | |
| Species = "Total mean", | |
| across(where(is.numeric), mean)) | |
| ) %>% | |
| tail() # for printing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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")) %>% |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) |
NewerOlder