Background:
Suppose I have an upper-level function that produces numeric results (e.g., .00235
, 2
, 8
etc.) and non-numeric results (e.g., "Not Possible"
, "Result is Unacceptable"
etc.).
QUESTION:
I have written a small lower-level function to control the decimal places (called decimal(see below)
) shown in R.
How can have my decimal()
function to work only when results of my upper-level function are numeric and otherwise let the result to appear as it is (i.e., as a character)?
## decimal display controller:
decimal <- function(x, k) format(round(x, k), nsmall = k, scientific =
ifelse(x >= 1e+05 || x <= -1e+05 || x <= 1e-05 & x >= -1e-05, T, F) )
## Example of use:
x = .000276573 ## HERE x is a numeric result, what if x = "Not possible"
decimal(x, 7)