corral: Corral a character vector

Description Usage Arguments Details Value Examples

Description

corral returns a corralled factor vector.

Usage

1
2
corral(x, method = c("size", "asis", "name"), groups = NULL,
  collect = "Other")

Arguments

x

A character vector (or any vector than can be coerced into a character).

method

A character string indicating the desired method of corralling with "size" being the default. This must be (an abbreviation of) one of the strings "size", "asis", or "name". See Details for more information.

groups

Either NULL (all values are kept distinct), a single number with the desired number of groups (floating numbers are truncated), or a vector of values to keep distinct. The default is NULL.

collect

Either NA or a character string that denotes the name that the "collected" values are given. The default is "Other".

Details

corral returns a corralled vector x. NA values in x are not grouped during the corralling process but are preserved in the output.

corral is designed to be readable from the function call. For example:

The output of corral is determined by the arguments method and groups.

corral offers a few different options for method:

corral accepts either numeric or character values for groups:

See the examples for some explicit illustration on how different combinations of method and groups result in different outputs.

Value

The output of corral is a corralled factor vector with the same length as x.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
set.seed(1337)
x <- sample(letters, 1e4, replace=TRUE)
summary(x)

#####
# Common use cases
#

# I want to factorize by sample size!
x_all <- corral(x, "size")
summary(x_all)
# All values are kept and ordered by the number of occurrences

# I want to factorize by sample size but only have 5 values!
x_5 <- corral(x, "size", groups=5)
summary(x_5)
# The four most common values are kept and
# everything else is combined into "Other"

# I want to factorize but keep only specific values!
x_bar <- corral(x, "asis", groups=c("b", "a", "r"))
summary(x_bar)
# The values "b", "a", and "r" are explicitly kept and
# leveled based on the order provided (i.e. "b" then "a" then "r")

# I want to change the collected values to NA rather than "Other"!
x_NA <- corral(x, "asis", groups=c("b", "a", "r"), collect=NA)
summary(x_NA)
# The values "b", "a", and "r" are factorized as in the previous example and
# the rest of the values are changed to NA

derek-damron/transform documentation built on May 15, 2019, 3:57 a.m.