/
imap.Rd
68 lines (58 loc) · 1.74 KB
/
imap.Rd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/imap.R
\name{imap}
\alias{imap}
\alias{imap_lgl}
\alias{imap_chr}
\alias{imap_int}
\alias{imap_dbl}
\alias{iwalk}
\title{Apply a function to each element of a vector, and its index}
\usage{
imap(.x, .f, ...)
imap_lgl(.x, .f, ...)
imap_chr(.x, .f, ...)
imap_int(.x, .f, ...)
imap_dbl(.x, .f, ...)
iwalk(.x, .f, ...)
}
\arguments{
\item{.x}{A list or atomic vector.}
\item{.f}{A function, specified in one of the following ways:
\itemize{
\item A named function, e.g. \code{paste}.
\item An anonymous function, e.g. \verb{\\(x, idx) x + idx} or
\code{function(x, idx) x + idx}.
\item A formula, e.g. \code{~ .x + .y}. You must use \code{.x} to refer to the
current element and \code{.y} to refer to the current index. Only recommended
if you require backward compatibility with older versions of R.
}}
\item{...}{Additional arguments passed on to the mapped function.
Note that the arguments that differ in each call come before \code{.f},
and the arguments that are the same come after \code{.f}.}
}
\value{
A vector the same length as \code{.x}.
}
\description{
\code{imap(x, ...)}, an indexed map, is short hand for
\code{map2(x, names(x), ...)} if \code{x} has names, or \code{map2(x, seq_along(x), ...)}
if it does not. This is useful if you need to compute on both the value
and the position of an element.
}
\examples{
imap_chr(sample(10), paste)
imap_chr(sample(10), \(x, idx) paste0(idx, ": ", x))
iwalk(mtcars, \(x, idx) cat(idx, ": ", median(x), "\n", sep = ""))
}
\seealso{
Other map variants:
\code{\link{lmap}()},
\code{\link{map2}()},
\code{\link{map_depth}()},
\code{\link{map_if}()},
\code{\link{map}()},
\code{\link{modify}()},
\code{\link{pmap}()}
}
\concept{map variants}