rowwise_*()
functions apply a function to each row of a data.frame.
The input to the function .f
is a single-row subset with the same class.
This is useful for handling subclasses of data.frame, such as sf
.
base::apply(.x, 1L, .f)
cannot do this because it converts the input to a matrix.
rowwise_mcmap*()
are parallelized versions of rowwise_map*()
.
Usage
rowwise_map(
.x,
.f,
mode = c("list", "integer", "double", "character", "logical")
)
rowwise_map_lgl(.x, .f)
rowwise_map_int(.x, .f)
rowwise_map_dbl(.x, .f)
rowwise_map_chr(.x, .f)
rowwise_map_vec(.x, .f, .ptype = NULL)
rowwise_mcmap(.x, .f, ...)
rowwise_mcmap_lgl(.x, .f, ...)
rowwise_mcmap_int(.x, .f, ...)
rowwise_mcmap_dbl(.x, .f, ...)
rowwise_mcmap_chr(.x, .f, ...)
rowwise_mcmap_vec(.x, .f, ..., .ptype = NULL)
Arguments
- .x
A data frame-like object.
- .f
A function to apply to each row.
- mode
Output type.
- .ptype
A prototype for the output vector.
- ...
Additional arguments passed to
parallel::mclapply()
.
Value
A vector of the same length as the number of rows in .x
.
The suffix of the function name denotes the output type.
*_vec()
applies purrr::list_simplify()
before returning.
Examples
df = data.frame(a = 1:2, b = 3:4)
rowwise_map(df, identity)
#> [[1]]
#> a b
#> 1 1 3
#>
#> [[2]]
#> a b
#> 2 2 4
#>
rowwise_map_lgl(df, is.data.frame)
#> [1] TRUE TRUE
rowwise_map_int(df, ncol)
#> [1] 2 2
rowwise_map_dbl(df, sum)
#> [1] 4 6
rowwise_map_chr(df, toString)
#> [1] "1, 3" "2, 4"
rowwise_map_vec(df, \(x) x$a + x$b * 1i)
#> [1] 1+3i 2+4i