def {svMisc} | R Documentation |
This function makes sure that a vector of a given mode and length is returned.
If the value provided is NULL, NA, or empty, a default value is used instead.
If length = NULL
, the length of the vector is not constrained, otherwise,
it is fixed (possibly cutting or recycling value
).
def(value, default = "", mode = "character", length = NULL)
value |
the value to pass with default |
default |
the default value to use, in case of NULL, NA, or length == 0 |
mode |
the mode of the result: character, logical, numeric, complex or factor. Other modes are NOT coerced! |
length |
the desired length of the returned vector; use length = NULL
(default) if you don't want to change the length of the vector, i.e., keep same
length as value . |
A vector of given mode and length, with either value
or default
.
Philippe Grosjean <phgrosjean@sciviews.org>
def(1:3, length = 5) # Convert into character and recycle def(0:2, mode = "logical") # Numbers to logical def(c("TRUE", "FALSE"), mode = "logical") # Text to logical def(NULL, "default text") # Default value used def(character(0), "default text") # Idem def(NA, 10, mode = "numeric", length = 2) # Vector of two numbers