fct2v {str2str}R Documentation

Factor to (Atomic) Vector

Description

fct2v converts a factor to an (atomic) vector. It allows the user to specify whether they want the factor to always return a character vector (simplify = TRUE), simplified if possible (simplify = FALSE), or just return the integer codes (codes = TRUE).

Usage

fct2v(fct, simplify = TRUE, codes = FALSE, check = TRUE)

Arguments

fct

factor.

simplify

logical vector of length 1 specifying whether R should attempt to simplify fct to typeof simplier than character (e.g., logical, integer, double). If FALSE, a character vector is always returned.

codes

logical vector of length 1 specifying whether the integer codes of fct should be returned. If codes = TRUE, then simplify is ignored.

check

logical vector of length 1 specifying whether to check the structure of the input arguments. For example, check whether fct is a factor. This argument is available to allow flexibility in whether the user values informative error messages (TRUE) vs. computational efficiency (FALSE).

Details

When simplify = TRUE, fct2v uses type.convert to try to simplify the factor. Note, missing values are assumed to be "NA" and decimals are assumed to be "."; however, "L" after a number is not interpreted as an integer specifier.

Value

(atomic) vector of the same length as fct. If codes = TRUE, then the returned vector is typeof integer containing the underlying factor codes. If codes = FALSE and simplify = FALSE, then the returned vector is typeof character containing the factor levels. If codes = FALSE, and simplify = TRUE, then the returned vector is the simpliest typeof possible without having to coerce any elements to NA. For example, if fct contains all integer numerals (e.g., "1", "2", "3", etc), then it will be converted to an integer vector. See examples.

Examples

fct2v(state.region)
fct2v(fct = factor(c("7.00001","8.54321","9.99999"))) # double
fct2v(fct = factor(c("7","8","9")), simplify = FALSE) # character
fct2v(fct = factor(c("7","8","9")), simplify = TRUE) # integer
fct2v(fct = factor(c("7","8","9")), codes = TRUE) # integer codes
fct2v(fct = factor(c("7L","8L","9L")),
   simplify = TRUE) # does not understand "L" for integers

[Package str2str version 1.0.0 Index]