Row-wise arithmetic operations

row_arith(.data, ...)

# S3 method for matrix
row_arith(
  .data,
  operator = c("+", "-", "*", "/"),
  cumulative = FALSE,
  output_mode,
  output_class,
  ...
)

# S3 method for data.frame
row_arith(
  .data,
  operator = c("+", "-", "*", "/"),
  cumulative = FALSE,
  output_mode,
  output_class,
  ...
)

Arguments

.data

A two-dimensional data structure.

...

Arguments passed on to op_ctrl

na_action

One of ("exclude", "pass"), possibly abbreviated. See stats::na.pass for semantics.

cols

A vector indicating which columns to consider for the operation. If NULL, all columns are used. If its length is 0, no columns are considered. Negative numbers, logical values, character vectors representing column names, and tidyselect::select_helpers are supported.

rows

Like cols but for row indices, and without tidyselect support.

operator

One of ("+", "-", "*", "/").

cumulative

Logical. Whether to return the cumulative operation.

output_mode

Passed to op_ctrl(). If missing, it will be inferred.

output_class

Passed to op_ctrl(). If missing, it will be inferred.

Details

Conceptually, this function takes each row, say x, and executes Reduce(operator, unlist(x)). x may be a list of values with different non-character types (like a data.frame row), NA values can be excluded, and results can be accumulated.

Examples

mat <- matrix(1L:9L, nrow = 3L, ncol = 3L) row_arith(mat, "-")
#> [1] -10 -11 -12
row_arith(mat, "-", cumulative = TRUE)
#> [,1] [,2] [,3] #> [1,] 1 -3 -10 #> [2,] 2 -3 -11 #> [3,] 3 -3 -12
df <- data.frame(bool = TRUE, int = 1L, double = 1.0, complex = 1+0i) # all columns promoted to complex row_arith(df, "*")
#> [1] 1+0i
row_arith(df, "/", cumulative = TRUE)
#> V1 V2 V3 V4 #> 1 1+0i 1+0i 1+0i 1+0i
# only promotion to integer is needed row_arith(df, "+", cols = 1:2)
#> [1] 2