mutate_pmap | R Documentation |
pmap
to create a new column in a data.frameSometimes you want to use mutate
to create a new column in a
data.frame, but that function doesn't work with vectors. One workaround is to
use a rowwise() %>% mutate()
, however that is quite slow. This
function lets you use the speed of pmap
in a mutate
-like syntax
(and returns the original data.frame with a new column, instead of just a
list). This works best if the column names of .data
match the
arguments expected by .f
.
mutate_pmap(.data, col_name, .f, ...)
.data |
A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). See Methods, below, for more details. |
col_name |
(unquoted character) the name of the new column you want to add |
.f |
A function, specified in one of the following ways:
|
... |
Additional arguments passed on to the mapped function. We now generally recommend against using # Instead of x |> map(f, 1, 2, collapse = ",") # do: x |> map(\(x) f(x, 1, 2, collapse = ",")) This makes it easier to understand which arguments belong to which function and will tend to yield better error messages. |
.data
with a new column named after col_name
.
## Not run:
# Create a column of charts:
# Create a custom plotting function
plot_starwars <- function(data) {
ggplot2::qplot(x = mass, y = height, data = data)
}
# Make a tibble with the starwars data in twice
tibble::tibble(data = list(
dplyr::starwars,
dplyr::starwars
)) %>%
mutate_pmap(
col_name = plot,
.f = plot_starwars
)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.