"

Today’s #rstats function I really should have tried before now: purrr::walk()

I love a good “for” loop, because it’s very readable, but walk() is more succinct and very readable - arguably more so!
Author
Affiliation

Building Stories with Data

Published

July 22, 2022

Say hi to the Penguins with me 👋🐧🐧🐧

# Setting things up
penguin_salute <- function(penguin) {
  print(glue::glue("Hi, {penguin}!"))
}

penguins <- c("Adelie", "Chinstrap", "Gentoo")

# Using a for loop
for (penguin in penguins) {
  penguin_salute(penguin)
}
Hi, Adelie!
Hi, Chinstrap!
Hi, Gentoo!
# Using purrr
purrr::walk(penguins, penguin_salute)
Hi, Adelie!
Hi, Chinstrap!
Hi, Gentoo!

P.S. Thanks dgkeyes for the tip!

Reuse

Citation

For attribution, please cite this work as:
Thompson, Cara. 2022. “Today’s #Rstats Function I Really Should Have Tried Before Now: `Purrr::walk()`.” July 22, 2022. https://www.cararthompson.com/posts/2022-07-22-todays-rstats-function-i-really/.