# Iteration

Author

Jeffrey R. Stevens

Published

March 29, 2023

1. Write a for loop that calculates the mean bill length for each species in the `penguins` data set (don’t use `group_by()`) and saves them as `species_means`.
``library(tidyverse)``
``````── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.0     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors``````
``````library(palmerpenguins)
penguin_species <- unique(penguins\$species)
species_means <- NA
for (i in penguin_species) {
species_means[i] <- penguins |>
filter(species == i) |>
summarise(mean_bill_length = mean(bill_length_mm, na.rm = TRUE)) |>
pull()
}
species_means <- species_means[!is.na(species_means)]``````
1. Turn #1 into a function called `species_mean` that lets the user determine which variable to calculate the mean over.
``````species_mean <- function(var) {
penguin_species <- unique(penguins\$species)
species_means <- NA
for (i in penguin_species) {
species_means[i] <- penguins |>
filter(species == i) |>
summarise(mean({{var}}, na.rm = TRUE)) |>
pull()
}
species_means <- species_means[!is.na(species_means)]
return(species_means)
}
species_mean(bill_length_mm)``````
``````   Adelie    Gentoo Chinstrap
38.79139  47.50488  48.83382 ``````
1. Create a list `penguins_island` that separates the penguins data by island.
``````penguins_island <- penguins |>
split(penguins\$island)``````
1. Apply `map()` to find the number of observations for each year.
``map(penguins_island, nrow)``
``````\$Biscoe
[1] 168

\$Dream
[1] 124

\$Torgersen
[1] 52``````
1. Apply `map()` to calculate the mean body weight for each island.
``map(penguins_island, ~ mean(.x\$body_mass_g, na.rm = TRUE))``
``````\$Biscoe
[1] 4716.018

\$Dream
[1] 3712.903

\$Torgersen
[1] 3706.373``````
1. Rework #5 to return a numeric vector with values rounded to 1 decimal place.
``map_dbl(penguins_island, ~ round(mean(.x\$body_mass_g, na.rm = TRUE), 1))``
``````   Biscoe     Dream Torgersen
4716.0    3712.9    3706.4 ``````