Learn R Programming

expss (version 0.7.1)

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

Description

These functions are intended for usage inside modify, modify_if, with and within functions. sum/mean/sd/median/max/min always 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. They don't remove NA.

Usage

sum_row(...)

sum_col(...)

mean_row(...)

mean_col(...)

sd_row(...)

sd_col(...)

median_row(...)

median_col(...)

max_row(...)

max_col(...)

min_row(...)

min_col(...)

apply_row(fun, ...)

apply_col(fun, ...)

any_in_row(...)

any_in_col(...)

all_in_row(...)

all_in_col(...)

Arguments

...

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

fun

custom function that will be applied to …

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.

See Also

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

Examples

Run this code
# NOT RUN {
## Inside example
iris = modify(iris,{
  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
modify(dfs, {
    b_total = sum_row(b_, b_1 %to% b_5)
})

# conditional modification
modify_if(dfs, test %in% 2:4, {
    b_total = sum_row(b_, b_1 %to% b_5)
})

# Examples from rowSums/colSums manual.
## Compute row and column sums for a matrix:
x = cbind(x1 = 3, x2 = c(4:1, 2:5))
sum_row(x); sum_col(x)
dimnames(x)[[1]] <- letters[1:8]
sum_row(x); sum_col(x); mean_row(x); mean_col(x)

# }

Run the code above in your browser using DataLab