dapply: Apply a Function to Subsets of Data Frame.

View source: R/utils.R

dapplyR Documentation

Apply a Function to Subsets of Data Frame.

Description

[Experimental]

dapply splits the data df into the subsets defined by f, and applies function FUN to each of the subset. All the results are row-binded and returned as data.frame object.

Usage

dapply(df, f, FUN, ...)

Arguments

df

(⁠data frame⁠)
data set to be divided into groups.

f

(factor or formula or list)
a factor in the sense that as.factor(f) defines the grouping, or a list of such factors in which case their interaction is used for the grouping. f can also be a formula of the form ~ g1 + ... + gk to split by the interaction of the variables ⁠g1, ..., gk⁠. This parameter is passed directly into split() function.

FUN

(function)
the function to be applied to each subset of df defined by f.

...

parameters passed to lapply(), which is used when applying a function FUN over groups defined by f.

Value

The data.frame object with results from FUN.

Examples

df <- data.frame(
  dose = c(0.1, 6, 6, 5, 0.1, 5, 6, 6),
  cohort = c("B", "B", "B", "A", "A", "A", "B", "B")
)

dapply(
  df,
  f = ~cohort,
  FUN = function(coh) {
    data.frame(my_cohort = coh$cohort[1], my_max = max(coh$dose))
  }
)

dapply(
  df,
  f = ~cohort,
  FUN = function(coh) {
    coh$dose <- sort(coh$dose, decreasing = TRUE)
    coh
  }
)

Roche/crmPack documentation built on April 20, 2024, 1:10 p.m.