Description Usage Arguments Details Value Examples
A generalization of ave()
that is easier to use with functions other than mean()
.
1 | generalized_ave(fun)
|
fun |
A function. |
This is a function factory that returns a function that behaves much like ave()
, except that it works more consistently with functions other than mean()
. The function returned by generalized_ave()
:
Allows further (named) arguments to be passed to fun
;
Bases the return value off of fun
, instead of x
.
Unlike ave()
, the names for x
are the only attributes of x
that are kept.
A function
function(x, f, ...)
,
where x
is a vector, f
is a factor that groups x
, and ...
are other arguments to pass to fun
.
x
serves the same role as in ave()
, with f
replacing ...
so that there is a way to pass further arguments to fun
. Unlike ave()
, the type of the return value is determined by the return value of fun
, which may not be the same as x
.
1 2 3 4 5 6 7 8 9 10 | generalized_ave(mean)(1:10, rep(1:2, 5)) # same as stats::ave(1:10, rep(1:2, 5))
generalized_ave(sum)(1:10, rep(1:2, 5)) # same as stats::ave(1:10, rep(1:2, 5), FUN = sum)
generalized_ave(function(x) x == max(x))(1:10, rep(1:2, 5)) # returns a logical
# use interaction to group by multiple factors
f <- rep(1:2, 5)
g <- rep(1:5, 2)
generalized_ave(mean)(1:10, interaction(f, g)) # same as stats::ave(1:10, f, g)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.