Learn R Programming

svMisc (version 0.9-60)

def: Define a vector of a given mode and length (possibly filling it with default values)

Description

This function makes sure that a vector of a given mode and length is returned. If the value provided is NULL, or empty, the default value is used instead. If length.out = NULL, the length of the vector is not constrained, otherwise, it is fixed (possibly cutting or recycling value).

Usage

def(value, default = "", mode = "character", length.out = NULL)

Arguments

value
the value to pass with default.
default
the default value to use, in case of NULL, or length(value) == 0.
mode
the mode of the resulting object: 'character', 'logical', 'numeric' (and, if you want to be more precise: 'double', 'integer' or 'single') or 'complex'. Although not being a mode by itself, you can also specify 'factor' to make sure the res
length.out
the desired length of the returned vector; use length.out = NULL (default) if you don't want to change the length of the vector.

Value

  • A vector of given mode and length, with either value or default.

concept

coercion and default values

See Also

mode, rep getTemp, assignTemp

Examples

Run this code
def(1:3, length.out = 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.out = 2) # Vector of two numbers

Run the code above in your browser using DataLab