imap()

  • imap() is like map2()except that .y is derived from names(.x) if named or seq_along(.x) if not.

  • These two produce the same result

imap_chr(.x = mtcars, 
         .f = ~ paste(.y, "has a mean of", round(mean(.x), 1))) |> 
head()
#>                        mpg                        cyl 
#>   "mpg has a mean of 20.1"    "cyl has a mean of 6.2" 
#>                       disp                         hp 
#> "disp has a mean of 230.7"   "hp has a mean of 146.7" 
#>                       drat                         wt 
#>   "drat has a mean of 3.6"     "wt has a mean of 3.2"

map2_chr(.x = mtcars, 
         .y = names(mtcars),
         .f = ~ paste(.y, "has a mean of", round(mean(.x), 1))) |> 
head()
#>                        mpg                        cyl 
#>   "mpg has a mean of 20.1"    "cyl has a mean of 6.2" 
#>                       disp                         hp 
#> "disp has a mean of 230.7"   "hp has a mean of 146.7" 
#>                       drat                         wt 
#>   "drat has a mean of 3.6"     "wt has a mean of 3.2"