Allows you to mutate "by row". this is most useful when a vectorized function doesn't exist.

mutate_rowwise(
  .df,
  ...,
  .keep = c("all", "used", "unused", "none"),
  .before = NULL,
  .after = NULL
)

Arguments

.df

A data.table or data.frame

...

Columns to add/modify

.keep

experimental: This is an experimental argument that allows you to control which columns from .df are retained in the output:

  • "all", the default, retains all variables.

  • "used" keeps any variables used to make new variables; it's useful for checking your work as it displays inputs and outputs side-by-side.

  • "unused" keeps only existing variables not used to make new variables.

  • "none", only keeps grouping keys (like transmute()).

.before, .after

Optionally indicate where new columns should be placed. Defaults to the right side of the data frame.

Examples

df <- data.table(x = 1:3, y = 1:3 * 2, z = 1:3 * 3)

# Compute the mean of x, y, z in each row
df %>%
  mutate_rowwise(row_mean = mean(c(x, y, z)))
#> # A tidytable: 3 × 4
#>       x     y     z row_mean
#>   <int> <dbl> <dbl>    <dbl>
#> 1     1     2     3        2
#> 2     2     4     6        4
#> 3     3     6     9        6

# Use c_across() to more easily select many variables
df %>%
  mutate_rowwise(row_mean = mean(c_across(x:z)))
#> # A tidytable: 3 × 4
#>       x     y     z row_mean
#>   <int> <dbl> <dbl>    <dbl>
#> 1     1     2     3        2
#> 2     2     4     6        4
#> 3     3     6     9        6