R purrr::walk() Function

The walk() function from the purrr package in R is used to perform actions for its side effects like writing files and printing plots and you are not interested in the return values.

Syntax

walk(.x, .f, ..., .progress = FALSE)

Parameters

  1. .x: It is a list or atomic vector.
  2. .f: It is a function.
  3. ...: Additional arguments passed on to the mapped function.
  4. .progress: Whether to show a progress bar. Use TRUE to turn on a basic progress bar.

Visual representation

Visual representation of walk() function

Example

library(purrr)

cars <- list("bmw", "audi", "mercedez")

walk(cars, print)

Output

[1] "bmw"
[1] "audi"
[1] "mercedez"

Difference between walk() and map() functions

The main difference between the walk() and map() functions is that the walk() function is used when you want to act on each element of a list or vector, such as printing or writing to a file. Still, you don’t want to return anything.

On the other hand, the map() function is used when you want to apply a function to each element of a list or vector and return the results as a new list or vector.

For example, if you have a list of numbers and want to square each number and return the results as a new list, you can use map() to apply the ^2 function to each number.

Leave a Comment