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.
Arguments
- df
(
data frame
)
data set to be divided into groups.- f
(
factor
orformula
orlist
)
a factor in the sense thatas.factor(f)
defines the grouping, or alist
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 variablesg1, ..., gk
. This parameter is passed directly intosplit()
function.- FUN
(
function
)
the function to be applied to each subset ofdf
defined byf
.- ...
parameters passed to
lapply()
, which is used when applying a functionFUN
over groups defined byf
.
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))
}
)
#> my_cohort my_max
#> 1 A 5
#> 2 B 6
dapply(
df,
f = ~cohort,
FUN = function(coh) {
coh$dose <- sort(coh$dose, decreasing = TRUE)
coh
}
)
#> dose cohort
#> 1 5.0 A
#> 2 5.0 A
#> 3 0.1 A
#> 4 6.0 B
#> 5 6.0 B
#> 6 6.0 B
#> 7 6.0 B
#> 8 0.1 B