Skip to content

Instantly share code, notes, and snippets.

View alejandrohagan's full-sized avatar
🙃
Working from home

Alejandro Hagan alejandrohagan

🙃
Working from home
  • Exxon Mobil Corporation
  • Spring, Texas
View GitHub Profile
@alejandrohagan
alejandrohagan / gist:a0e1443c1799c264b6213abbe0c7ca76
Created March 1, 2025 18:03
how to apply function to nested dataset when nest_by column is needed in function
library(tidyverse)
fun <- function(x) {
if (any(x$Species == "setosa")) {
tail(x, n = 3) |> select(Petal.Length)
} else {
head(x, n = 3) |> select(Petal.Length)
}
}
library(tidyverse)
#Flip 100 coins, labeled 1 through 100. Alice checks the coins in order (1, 2, 3, …) while Bob checks the odd-labeled coins, #then the even-labeled ones (so 1, 3, 5, …, 99, 2, 4, 6, …). Who is more likely to see two heads *first*?
library(tidyverse)
flip_coin <- function(){
flip_coin_tbl <- tibble(
# https://gilkalai.wordpress.com/2009/07/08/test-your-intuition-7/
#Consider the following game: you have a box that contains one white ball and one black ball.
#You choose a ball at random and then return it to the box.
#If you chose a white ball then a white ball is added to the box, and if you chose a black ball then a black ball is added to the box.
#Played over time, what is the probability that more than 80 percents of the chosen balls are white?
library(tidyverse)
## make simulation functions -- I couldhave done it one go but just made it more modular
@alejandrohagan
alejandrohagan / gist:912cafd3b2efc20f22dbab2d78619bf4
Created September 3, 2024 21:38
2023-09-03-test-your-intuition-56-fifteen-boxes-puzzle
## riddle here https://gilkalai.wordpress.com/2024/09/03/test-your-intuition-56-fifteen-boxes-puzzle/
library(tidyverse)
choose_box <- function(){
# create search order
andrew <- 1:15
#You have 3 urns, each containing 100 balls.
#In the first two, 99 of the balls are red and the last is green.
#In the third urn, all 100 balls are red.
#You choose one of the urns at random and remove 99 randomly chosen balls from it; they’re all red.
#The last is probably?
library(tidyverse)
pick_ball <- function(){
##You have 10000 coins. 9999 of them are fair; one is rigged so that it always lands on heads.
##You choose a coin at random and flip it 10 times; it’s heads all ten times.
## what is likelihood that the coin is rigged?
ht_list <- rep(list(c("H", "T")), 9999)
ht_list[[10000]] <- c("H", "H")
@alejandrohagan
alejandrohagan / gist:2eccb97c3e3f872027df3a5a4d750159
Last active December 3, 2023 04:09
Simulation of Riddler Gym
hi!
Thank you again for taking a look -- I'm trying to improve my simulation skills and curious where I might have the logic or intuition wrong with this riddler exercise
```{r}
library(tidyverse)
total_minutes <- 3*180