binarizeCategoricalVariable: Turn a categorical variable into a set of binary indicators

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/Functions.R

Description

Given a categorical variable, this function creates a set of indicator variables for the various possible sets of levels.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
binarizeCategoricalVariable(
   x,
   levelOrder = NULL,
   ignore = NULL,
   minCount = 3,
   val1 = 0, val2 = 1,
   includePairwise = TRUE,
   includeLevelVsAll = FALSE,
   dropFirstLevelVsAll = FALSE,
   dropUninformative = TRUE,
   namePrefix = "",
   levelSep = NULL,
   nameForAll = "all",
   levelSep.pairwise = if (length(levelSep)==0) ".vs." else levelSep,
   levelSep.vsAll = if (length(levelSep)==0) 
                       (if (nameForAll=="") "" else ".vs.") else levelSep,
   checkNames = FALSE,
   includeLevelInformation = TRUE)

Arguments

x

A vector with categorical values.

levelOrder

Optional specification of the levels (unique values) of x. Defaults to sorted unique values of x, but can be used to only include a subset of the existing levels as well as to specify the order of the levels in the output variables.

ignore

Optional specification of levels of x that are to be ignored. Note that the levels are ignored only when deciding which variables to include in the output; the samples with these values of x will be included in "all" in indicators of level vs. all others.

minCount

Levels of x for which there are fewer than minCount elements will be ignored.

val1

Value for the lower level in binary comparisons.

val2

Value for the higher level in binary comparisons.

includePairwise

Logical: should pairwise binary indicators be included? For each pair of levels, the indicator is val1 for the lower level (earlier in levelOrder), val2 for the higher level and NA otherwise.

includeLevelVsAll

Logical: should binary indicators for each level be included? The indicator is val2 where x equals the level and val1 otherwise.

dropFirstLevelVsAll

Logical: should the column representing first level vs. all be dropped? This makes the resulting matrix of indicators usable for regression models.

dropUninformative

Logical: should uninformative (constant) columns be dropped?

namePrefix

Prefix to be used in column names of the output.

nameForAll

When naming columns that represent a level vs. all others, nameForAll will be used to represent all others.

levelSep

Separator for levels to be used in column names of the output. If NULL, pairwise and level vs. all indicators will use different level separators set by levelSep.pairwise and levelSep.vsAll.

levelSep.pairwise

Separator for levels to be used in column names for pairwise indicators in the output.

levelSep.vsAll

Separator for levels to be used in column names for level vs. all indicators in the output.

checkNames

Logical: should the names of the output be made into syntactically correct R language names?

includeLevelInformation

Logical: should information about which levels are represented by which columns be included in the attributes of the output?

Details

The function creates two types of indicators. The first is one level (unique value) of x vs. all others, i.e., for a given level, the indicator is val2 (usually 1) for all elements of x that equal the level, and val1 (usually 0) otherwise. Column names for these indicators are the concatenation of namePrefix, the level, nameSep and nameForAll. The level vs. all indicators are created for all levels that have at least minCounts samples, are present in levelOrder (if it is non-NULL) and are not included in ignore.

The second type of indicator encodes binary comparisons. For each pair of levels (both with at least minCount samples), the indicator is val2 (usually 1) for the higher level and val1 (usually 0) for the lower level. The level order is given by levelOrder (which defaults to the sorted levels of x), assumed to be sorted in increasing order. All levels with at least minCount samples that are included in levelOrder and not included in ignore are included.

Value

A matrix containing the indicators variabels, one in each column. When includeLevelInformation is TRUE, the attribute includedLevels is a table with one column per output column and two rows, giving the two levels (unique values of x) represented by the column.

Author(s)

Peter Langfelder

See Also

Variations and wrappers for this function: binarizeCategoricalColumns for binarizing several columns of a matrix or data frame

Examples

1
2
3
4
5
6
7
set.seed(2);
x = sample(c("A", "B", "C"), 15, replace = TRUE);
out = binarizeCategoricalVariable(x, includePairwise = TRUE, includeLevelVsAll = TRUE);
data.frame(x, out);
attr(out, "includedLevels")
# A different naming for level vs. all columns
binarizeCategoricalVariable(x, includeLevelVsAll = TRUE, nameForAll = "");

Example output

Loading required package: dynamicTreeCut
Loading required package: fastcluster

Attaching package: 'fastcluster'

The following object is masked from 'package:stats':

    hclust



Attaching package: 'WGCNA'

The following object is masked from 'package:stats':

    cor

   x B.vs.A C.vs.A C.vs.B A.vs.all B.vs.all C.vs.all
1  A      0      0     NA        1        0        0
2  C     NA      1      1        0        0        1
3  B      1     NA      0        0        1        0
4  A      0      0     NA        1        0        0
5  C     NA      1      1        0        0        1
6  C     NA      1      1        0        0        1
7  A      0      0     NA        1        0        0
8  C     NA      1      1        0        0        1
9  B      1     NA      0        0        1        0
10 B      1     NA      0        0        1        0
11 B      1     NA      0        0        1        0
12 A      0      0     NA        1        0        0
13 C     NA      1      1        0        0        1
14 A      0      0     NA        1        0        0
15 B      1     NA      0        0        1        0
        B.vs.A C.vs.A C.vs.B A.vs.all B.vs.all C.vs.all
Value.0 "A"    "A"    "B"    "all"    "all"    "all"   
Value.1 "B"    "C"    "C"    "A"      "B"      "C"     
      B.vs.A C.vs.A C.vs.B A B C
 [1,]      0      0     NA 1 0 0
 [2,]     NA      1      1 0 0 1
 [3,]      1     NA      0 0 1 0
 [4,]      0      0     NA 1 0 0
 [5,]     NA      1      1 0 0 1
 [6,]     NA      1      1 0 0 1
 [7,]      0      0     NA 1 0 0
 [8,]     NA      1      1 0 0 1
 [9,]      1     NA      0 0 1 0
[10,]      1     NA      0 0 1 0
[11,]      1     NA      0 0 1 0
[12,]      0      0     NA 1 0 0
[13,]     NA      1      1 0 0 1
[14,]      0      0     NA 1 0 0
[15,]      1     NA      0 0 1 0
attr(,"includedLevels")
        B.vs.A C.vs.A C.vs.B A   B   C  
Value.0 "A"    "A"    "B"    ""  ""  "" 
Value.1 "B"    "C"    "C"    "A" "B" "C"

WGCNA documentation built on March 1, 2021, 1:05 a.m.