apply_per_group: Apply functions

Description Usage Arguments Value Author(s) Examples

View source: R/apply.R

Description

apply_per_group : Apply a function to each group which is a subset of samples and binders stratified by the variables in @sinfo and @binder. The variable names are given by the argument by_s and by_b.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
apply_per_group(baf, FUN, ..., by_s = NULL, by_b = NULL, passBAf = FALSE)

sapply_per_group(
  baf,
  FUN,
  ...,
  by_s = NULL,
  by_b = NULL,
  b_simplify = T,
  s_simplify = b_simplify
)

Arguments

baf

an object of BAf-class

FUN

the function

...

the arguments that will be passed to FUN

by_s, by_b

by which the samples (by_s) or binders (by_b) are grouped. The FUN is applied to each group and the results are combined. This can be given by 1) a character string of a column name in @sinfo or @binder, and 2) a factor vector that can be used in the stratification. If it is NULL as the default, there will be no stratification. So, the FUN will be applied to entire data.

passBAf

if BAf object instead of matrix in @.Data is passed when applying the FUN.

b_simplify

if the output should be simplified like simplify for sapply

s_simplify

if the output within stratified group by the by_s should be simplified.

Value

an object of BAf-class

Author(s)

Mun-Gwan Hong <mun-gwan.hong@scilifelab.se>

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
data(sba)
summary(sba)

# mean per plate and assay
apply_per_group(sba, mean, by_s= "plate", by_b= "assay")

# calculate median after stratifying samples by "plate" and "sex"
apply_per_group(sba, median, na.rm= TRUE, by_s= c("plate", "sex"))

# verify the first median computed by above line
median(sba@.Data[with(sba@sinfo, plate == "1" & !is.na(sex) & sex == "female"), ])

# compute coefficients of variation (CV) of \code{MIX_1} wells
apply_per_group(sba[sba@sinfo$cohort == "MIX_1", 1:5], FUN= function(j) {
    apply(j, 2, function(k) sd(k, na.rm= TRUE)/mean(k, na.rm= TRUE))
})

Rundmus/BAf-R_package documentation built on May 18, 2020, 12:59 p.m.