library(tidyverse)── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.1 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.3 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(purrrfect)
Attaching package: 'purrrfect'
The following objects are masked from 'package:base':
replicate, tabulate
N <- 100000
p_head <- 0.4
( replicate(N, sample(c('H','T'), 8, prob = c(p_head, 1-p_head), replace = TRUE), .as = flips)
%>% mutate(num_heads = map_dbl(flips, \(x) sum(x=='H')),
at_least_four_heads = map_lgl(flips, \(x) sum(x=='H') >=4 ))
%>% summarize(p_at_least_four_heads = mean(at_least_four_heads))
)# A tibble: 1 × 1
p_at_least_four_heads
<dbl>
1 0.406
( replicate(N, sample(c('H','T'), 8, prob = c(p_head, 1-p_head), replace = TRUE), .as = flips)
%>% mutate(last_two_heads = map_lgl(flips, \(x) all(tail(x, 2) == 'H')))
%>% summarize(p_last_two_heads = mean(last_two_heads))
)# A tibble: 1 × 1
p_last_two_heads
<dbl>
1 0.162