Skip to content

rearrange list of steps in iteration #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions episodes/superspreading-simulate.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -456,28 +456,28 @@ First, let's sketch how we use `purrr::map()` with `epichains::simulate_chains()
- `.x`, with a vector of numbers, and
- `.f`, a function to iterate to each vector value.

The code chunk below

```r
map(
# vector of numbers (simulation IDs)
.x = seq_len(number_simulations),
# function to iterate to each simulation ID number
.f = function(sim) {
simulate_chains(...) %>%
# creates a column with the simulation ID number
mutate(simulation_id = sim)
}
) %>%
# combine list outputs (for each simulation ID) into a single data frame
list_rbind()
# steps:
# seq_len() creates a vector with sequence of numbers (simulation IDs from 1 to 1000) and
# function(sim) iterates {epichains} to each simulation ID number, then
# dplyr::mutate() adds a column to the <epichains> output with the simulation ID number.
# purrr::list_rbind() combines all the list class outputs (for each simulation ID) into a single data frame.
purrr::map(.x = seq_len(number_simulations), .f = function(sim) {
epichains::simulate_chains(...) %>% dplyr::mutate(simulation_id = sim)
}) %>%
purrr::list_rbind()
# pseudo code: do not run.
```

The `sim` element is placed to register the iteration number (**simulation ID**) as a new column in the `<epichains>` output. The `purrr::list_rbind()` function aims to combine all the list outputs from `map()`.

**Why a dot (`.`) as a prefix?** In the [tidy design principles](https://design.tidyverse.org/dots-prefix.html) book we have a chapter on the dot prefix!
**Why a dot (`.`) as a prefix before x and f arguments?** In the [tidy design principles](https://design.tidyverse.org/dots-prefix.html) book we have a chapter on the dot prefix!

:::::::::::::::::::::::::::::::

Now, we are prepared to use `map()` to repeatedly simulate from `simulate_chains()` and store in a vector from 1 to `r number_simulations`:
Now, we are prepared to use `purrr::map()` to repeatedly simulate from `simulate_chains()` and store in a vector from 1 to `r number_simulations`:

```{r}
set.seed(33)
Expand Down
Loading