Get means, sums and possibly other functions for a select number of columns using dplyr::select-style column selection.

row_sums(data, ..., na_rm = FALSE, varname = "sum")

row_means(data, ..., na_rm = FALSE, varname = "mean")

row_min(data, ..., na_rm = FALSE, varname = "min")

row_max(data, ..., na_rm = FALSE, varname = "max")

row_apply(data, ..., .fun, varname = "var")

Arguments

data

A data frame.

...

The columns to sum, take the mean of, etc. Required.

na_rm

Whether to remove NA values or not. Default is FALSE.

varname

The column name of the sums means etc. as a character string.

.fun

The function to apply.

Value

A data frame with a new column representing the sums, means or whatever.

Details

Simple wrappers for applying rowwise operations only for selected columns within the tidyverse approach to data processing. The row_apply function requires a function that already works on rows.

Examples

library(tidyext) library(dplyr) d = data.frame(x = 1:3, y = 4:6, z = 7:9) d %>% row_sums(x:y)
#> x y z sum #> 1 1 4 7 5 #> 2 2 5 8 7 #> 3 3 6 9 9
d %>% row_means(matches('x|z'))
#> x y z mean #> 1 1 4 7 4 #> 2 2 5 8 5 #> 3 3 6 9 6
d %>% row_max(matches('x|y'))
#> # A tibble: 3 x 3 #> x y max #> <int> <int> <int> #> 1 1 4 4 #> 2 2 5 5 #> 3 3 6 6
row_apply( d , everything(), .fun = function(x) apply(x, 1, paste, collapse = '') )
#> x y z var #> 1 1 4 7 147 #> 2 2 5 8 258 #> 3 3 6 9 369