make.fun_if {quest} | R Documentation |
Make a Function Conditional on Frequency of Observed Values
Description
make.fun_if
makes a function that evaluates conditional on a specified
minimum frequency of observed values. Within the function, if the frequency
of observed values is less than (or equal to) ov.min
, then
false
is returned rather than the return value.
Usage
make.fun_if(
fun,
...,
ov.min.default = 1,
prop.default = TRUE,
inclusive.default = TRUE,
false = NA
)
Arguments
fun |
function that takes an atomic vector as its first argument. The
first argument does not have to be named "x" within |
... |
additional arguments with parameters to |
ov.min.default |
numeric vector of length 1 specifying what the default
should be for the argument |
prop.default |
logical vector of length 1 specifying what the default
should be for the argument |
inclusive.default |
logical vector of length 1 speicfying what the
default should be for the argument |
false |
vector of length 1 specifying what should be returned if the
observed values condition is not met within the returned function. The
default is NA. Whatever the value is, it will be coerced to the same mode
as |
Value
function that takes an atomic vector x
as its first argument,
...
as other arguments, ending with ov.min
, prop
, and
inclusive
as final arguments with defaults specified by
ov.min.default
, prop.default
, and inclusive.default
,
respectively.
See Also
Examples
# SD
sd_if <- make.fun_if(fun = sd, na.rm = TRUE) # always have na.rm = TRUE
sd_if(x = airquality[[1]], ov.min = .75) # proportion of observed values
sd_if(x = airquality[[1]], ov.min = 116,
prop = FALSE) # count of observed values
sd_if(x = airquality[[1]], ov.min = 116, prop = FALSE,
inclusive = FALSE) # not include ov.min values itself
# skewness
skew_if <- make.fun_if(fun = psych::skew, type = 1) # always have type = 1
skew_if(x = airquality[[1]], ov.min = .75) # proportion of observed values
skew_if(x = airquality[[1]], ov.min = 116,
prop = FALSE) # count of observed values
skew_if(x = airquality[[1]], ov.min = 116, prop = FALSE,
inclusive = FALSE) # not include ov.min values itself
# mode
popular <- function(x) names(sort(table(x), decreasing = TRUE))[1]
popular_if <- make.fun_if(fun = popular) # works with character vectors too
popular_if(x = c(unlist(dimnames(HairEyeColor)), rep.int(x = NA, times = 10)),
ov.min = .50)
popular_if(x = c(unlist(dimnames(HairEyeColor)), rep.int(x = NA, times = 10)),
ov.min = .60)