Description Usage Arguments Details Value Examples
A wrapper for factor with enhanced control.
1 2 |
x |
a vector of data, usually taking a small number of distinct values. |
levels |
an optional vector of the values (as character strings) that
|
labels |
either an optional character vector of labels for the
levels (in the same order as |
exclude |
a vector of values to be excluded when forming the
set of levels. This should be of the same type as |
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 |
cfactor wraps factor but provides enhanced
control. The enhanced control has the following elements:
more warnings: NAs are never produced silently. Empty levels
also produce a warning. If levels and labels intersect, a
warning is produced too.
flexible order detection: By default, the order of the levels is
given by the order of the numerical elements in x. See below.
Levels of the factor or determined by the levels argument. If
levels is missing, the order of the levels is determined as
follows:
If sep is set to NULL: Levels are determined by
sorting the unique values in x, which is the same behaviour as in
factor.
If sep is set to NA, the order is given by first
appearance in x.
Otherwise,
If every value in x contains numbers: Levels are determined
by sorting the numerical values preceeding the separators indicated in
sep.
If not every value in x contains numbers: Levels are
determined by sorting the unique values in x, which is the same
behaviour as in factor and corresponds to setting sep
to NULL.
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.
See factor.
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.