Learn R Programming

expss (version 0.11.6)

sum_row: Compute sum/mean/sd/median/max/min/custom function on rows/columns

Description

These functions are intended for usage inside let, and let_if. sum/mean/sd/median/max/min by default omits NA. any_in_* checks existence of any TRUE in each row/column. It is equivalent of any applied to each row/column. all_in_* is equivalent of all applied to each row/column.

Usage

sum_row(..., na.rm = TRUE)

sum_col(..., na.rm = TRUE)

mean_row(..., na.rm = TRUE)

mean_col(..., na.rm = TRUE)

sd_row(..., na.rm = TRUE)

sd_col(..., na.rm = TRUE)

median_row(..., na.rm = TRUE)

median_col(..., na.rm = TRUE)

max_row(..., na.rm = TRUE)

max_col(..., na.rm = TRUE)

min_row(..., na.rm = TRUE)

min_col(..., na.rm = TRUE)

apply_row(fun, ...)

apply_col(fun, ...)

any_in_row(..., na.rm = TRUE)

any_in_col(..., na.rm = TRUE)

all_in_row(..., na.rm = TRUE)

all_in_col(..., na.rm = TRUE)

Value

All functions except apply_* return numeric vector of length equals the number of argument columns/rows. Value of apply_* depends on supplied fun function.

Arguments

...

data. Vectors, matrixes, data.frames, list. Shorter arguments will be recycled.

na.rm

logical. Contrary to the base 'sum' it is TRUE by default. Should missing values (including NaN) be removed?

fun

custom function that will be applied to ...

See Also

%to%, count_if, sum_if, mean_if, median_if, sd_if, min_if, max_if

Examples

Run this code
iris = iris %>% 
    let( 
        new_median = median_row(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width),
        new_mean = mean_row(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)
    )
  
dfs = data.frame(
    test = 1:5,
    aa = rep(10, 5),
    b_ = rep(20, 5),
    b_1 = rep(11, 5),
    b_2 = rep(12, 5),
    b_4 = rep(14, 5),
    b_5 = rep(15, 5) 
)

# calculate sum of b* variables
dfs %>% 
    let( 
        b_total = sum_row(b_, b_1 %to% b_5)
    ) %>% 
    print()

# conditional modification
dfs %>% 
    let_if(test %in% 2:4, 
        b_total = sum_row(b_, b_1 %to% b_5)
    ) %>% 
    print()


Run the code above in your browser using DataLab