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, ... )
| .data | A two-dimensional data structure. |
|---|---|
| ... | Arguments passed on to
|
| operator | One of ("+", "-", "*", "/"). |
| cumulative | Logical. Whether to return the cumulative operation. |
| output_mode | Passed to |
| output_class | Passed to |
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.
#> [1] -10 -11 -12row_arith(mat, "-", cumulative = TRUE)#> [,1] [,2] [,3] #> [1,] 1 -3 -10 #> [2,] 2 -3 -11 #> [3,] 3 -3 -12df <- data.frame(bool = TRUE, int = 1L, double = 1.0, complex = 1+0i) # all columns promoted to complex row_arith(df, "*")#> [1] 1+0irow_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