make.fun_if: Make a Function Conditional on Frequency of Observed Values

Description Usage Arguments Value See Also Examples

View source: R/quest_functions.R

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

1
2
3
4
5
6
7
8
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 fun, but it will be named "x" in the returned function.

...

additional arguments with parameters to fun. This would be similar to impute in sum_if. However in the current version of make.fun_if, the parameters you provide will always be used within the returned function and cannot be specified by the user of the returned function. Unfortunately, I cannot figure out how to include user-specified arguments (with defaults) within the returned function other than ov.min.default, prop.default, and inclusive.default.

ov.min.default

numeric vector of length 1 specifying what the default should be for the argument ov.min within the returned function, which specifies the minimum frequency of observed values required. If prop = TRUE, then this is a decimal between 0 and 1. If prop = FALSE, then this is a integer between 0 and length(x).

prop.default

logical vector of length 1 specifying what the default should be for the argument prop within the returned function, which specifies whether ov.min should refer to the proportion of observed values (TRUE) or the count of observed values (FALSE).

inclusive.default

logical vector of length 1 speicfying what the default should be for the argument inclusive within the returned function, which specifies whether the function should be evaluated if the frequency of observed values is exactly equal to ov.min.

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 x within the returned function.

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

sum_if mean_if

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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)

quest documentation built on Sept. 10, 2021, 5:07 p.m.

Related to make.fun_if in quest...