Convert factor to a factor label
Usage
factor2label(
x,
valuesL = NULL,
types = c("count", "none"),
aggFun = mean,
digits = 3,
big.mark = ",",
itemName = "items",
...
)Arguments
- x
factor vector
- valuesL
optional list of numeric values, where each vector has the same length as
x. If it is not a factor, it is converted to factor, usingjamba::mixedSort()to order the factor levels.- types
character value indicating the summary to use for the input factor
x.- aggFun
summary function used for each vector of numeric values in
valuesLwhen supplied.- digits, big.mark
arguments passed to
base::format()to create a suitable text label.- itemName
character vector indicating the name to associate to counts.
- ...
additional arguments are ignored.
Value
factor vector with the same length as input x but
where the levels also include summary information, such
as the count of each factor level.
Details
This function is intended to take a vector of factor levels and convert to labels using summary statistics. For example by default it will add the number of items for each factor level.
This function is intended to help create useful ordered factor labels that can be used in a ggplot2 visualization.
See also
Other Internal utility functions:
combineGRcoverage(),
compressPolygonM(),
df2colorSub(),
dfWide2segments(),
escapeWhitespaceRegexp(),
geomean(),
intercalate(),
internal_junc_score(),
jamGeomean(),
list2im(),
shrinkMatrix(),
simplifyXY(),
stackJunctions(),
strsplitOrdered()
Examples
x <- factor(rep(letters[1:5], c(2,4,3,2,1)));
levels(factor2label(x));
#> [1] "a (2 items)" "b (4 items)" "c (3 items)" "d (2 items)" "e (1 items)"
factor2label(x);
#> a a b b b b
#> a (2 items) a (2 items) b (4 items) b (4 items) b (4 items) b (4 items)
#> c c c d d e
#> c (3 items) c (3 items) c (3 items) d (2 items) d (2 items) e (1 items)
#> Levels: a (2 items) b (4 items) c (3 items) d (2 items) e (1 items)