Skip to contents

This function computes the median value for each feature.

See the Details section below for further information.

Usage

rowMedians(x, g = NULL)

Arguments

x

matrix or data.frame, where rows are features and columns are observations.

g

(optional) vector or factor object giving the group for the corresponding elements of x.

Value

A vector of length nrow(x) containing the computed ratios. If g is provided, a matrix with ratios for each class as column vectors is returned.

Details

If g = NULL, for each feature the median is computed via rowMedians.

If g is provided, the median per group is computed via median.

Author

Alessandro Barberis

Examples

#Seed
set.seed(1010)

#Define row/col size
nr = 5
nc = 10

#Data
x = matrix(
 data = sample.int(n = 100, size = nr*nc, replace = TRUE),
 nrow = nr,
 ncol = nc,
 dimnames = list(
   paste0("f",seq(nr)),
   paste0("S",seq(nc))
 )
)

#Grouping variable
g = c(rep("a", nc/2), rep("b", nc/2))

#Medians
rowMedians(x)
#>   f1   f2   f3   f4   f5 
#> 55.5 42.5 66.5 44.0 25.0 

#Medians by group
rowMedians(x = x, g = g)
#>     a  b
#> f1 75 35
#> f2 39 46
#> f3 75 63
#> f4 62 29
#> f5 28 22