Description Usage Arguments Value See Also Examples

View source: R/quest_functions.R

`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.

1 2 3 4 5 6 7 8 | ```
make.fun_if(
fun,
...,
ov.min.default = 1,
prop.default = TRUE,
inclusive.default = TRUE,
false = NA
)
``` |

`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 |

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.

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)
``` |

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.