force.scalar: Force argument to be a numeric type with length one

View source: R/NCmisc.R

force.scalarR Documentation

Force argument to be a numeric type with length one

Description

Sometimes arguments must be numeric, scalar and within a certain range. Rather than using many if statements, this will do everything possible to coerce input to a scalar, failing that will replace with a default value. Can also provide a maximum and minimum range that the result must lie within.

Usage

force.scalar(x, default = 1, min = -10^10, max = 10^10)

Arguments

x

the object to ensure is a scalar

default

the value to revert to if the format of x is illegal

min

a lower bound for the output, anything below this is set to min

max

an upper bound for the output, anything above this is set to max

Value

the object x if already legal, first element if a vector, the min or max value if x is outside the specified bounds, or the value of default otherwise

See Also

force.percentage

Examples

force.scalar(1.5)
force.scalar(NULL,default=.5)
force.scalar(NA,default=.4,min=5,max=10) # default is outside range!
force.scalar(rnorm(1000))
force.scalar(101,max=50)
force.scalar(list(0.4,1,2,3,4,"test"))
force.scalar(data.frame(test=c(1,2,3),name=c("test","me","few")))
force.scalar(Inf)

NCmisc documentation built on Oct. 17, 2022, 5:09 p.m.