Learn R Programming

NCmisc (version 1.2.0)

force.scalar: 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)

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

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

See Also

force.percentage

Examples

Run this code
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)

Run the code above in your browser using DataLab