View source: R/S05_Statistics.R
statistic | R Documentation |
This function robustly computes a statistic
over a vector of values. The user can
specify what to return if the vector
is empty, values to include or exclude,
and how to treat missing values. Useful in
combination of functions like
aggregate
or
dplyr's summarise
.
statistic(
x,
f = length,
include = NULL,
exclude = NULL,
na.rm = T,
default = NA,
...
)
x |
A vector. |
f |
A function that takes the vector
|
include |
A logical vector matching
length to the vector |
exclude |
A vector of unique cases to match and exclude. |
na.rm |
Logical; if |
default |
The default value to
return if |
... |
Additional arguments to
pass to the function |
A computed statistic, output from the user-supplied function.
# Examples using the 'iris' data set
data("iris")
# Default
statistic(iris$Sepal.Length)
# User-specified statistic
statistic(iris$Sepal.Length, f = mean)
# Include subset of cases
statistic(iris$Sepal.Length,
f = mean,
include = iris$Species == "setosa"
)
# Custom function
statistic(iris$Species, f = function(x) mean(x %in% "setosa"))
# Exclude unique cases
statistic(iris$Species,
f = function(x) mean(x %in% "setosa"),
exclude = "virginica"
)
# If empty vector supplied, user-specified default value returned
statistic(iris$wrong_name, default = 0)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.