cfactor: Factor

Description Usage Arguments Details Value Examples

View source: R/cfactor.R

Description

A wrapper for factor with enhanced control.

Usage

1
2
cfactor(x, levels, labels = levels, exclude = NA, ordered = is.ordered(x),
  nmax = NA, sep = c("-", "to"))

Arguments

x

a vector of data, usually taking a small number of distinct values.

levels

an optional vector of the values (as character strings) that x might have taken. If missing (the default), the levels are determined by sorting the numerical elements in x. This is not as in factor. See 'Details'.

labels

either an optional character vector of labels for the levels (in the same order as levels after removing those in exclude), or a character string of length 1.

exclude

a vector of values to be excluded when forming the set of levels. This should be of the same type as x, and will be coerced if necessary.

ordered

logical flag to determine if the levels should be regarded as ordered (in the order given).

nmax

an upper bound on the number of levels; see ‘Details’.

sep

A character vector giving all strings that are used to separate the lower numerical boundary of a range from the upper numerical boundary within x. See 'Details' for more information.

Details

cfactor wraps factor but provides enhanced control. The enhanced control has the following elements:

Levels of the factor or determined by the levels argument. If levels is missing, the order of the levels is determined as follows:

Apart from the newly introduced argument sep, cfactor has the same arguments and defaults as factor.
Where levels is not supplied, unique is called. Since factors typically have quite a small number of levels, for large vectors x it is helpful to supply nmax as an upper bound on the number of unique values.

Value

See factor.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
## Not run: 
## create warnings with unused and removed levels
string <- c("a", "b", "c")
cfactor(string, levels = c("b", "c", "d"))
## End(Not run)

## detecting levels: compare factor and cfactor
hard_to_detect <- c("EUR 21 - EUR 22", "EUR 100 - 101", 
"EUR 1 - EUR 10", "EUR 11 - EUR 20")
factor(hard_to_detect, ordered = TRUE)
cfactor(hard_to_detect, ordered = TRUE)

jonmcalder/refactor documentation built on Nov. 16, 2020, 3:46 a.m.