mix.binary: Binary coding of categorical variables

Description Usage Arguments Details Value Author(s) Examples

View source: R/mixtools.R

Description

This function encodes categorical variables (e.g. columns of type 'factor' or 'character'). U new columns are created per each such column, where U is the number of unique instances of that column. The new columns are named OriginalColumnName_U1, OriginalColumnName_U2, etc.

Usage

1

Arguments

x

A data.frame or a matrix where categorical columns are to be binary coded. Categorical columns are assumed to be all non-numeric fields.

Details

A function that codes categorical variables in a dataset into binary variables. This is done in the following manner: e.g. x = red, green, blue, green –> x_new = 1,0,0, 0,1,0, 0,0,1, 0,1,0 where the dimensions in x_new are is_red, is_green and is_blue

Value

The function returns a data.frame, where categorical variables have been replaced with 0/1-binary fields, and numeric fields have been left untouched. Notice that the order of the columns may not be the original.

Author(s)

Teemu Daniel Laajala <teelaa@utu.fi>

Examples

1
2
3
4
5
6
7
8
9
data(vcapwide)

ex <- mix.binary(vcapwide[,c("Group", "CastrationDate")])
apply(ex, MARGIN=1, FUN=sum) 
# Notice that each row sums to 2, as two categorical variables were binary coded 
# and no missing values were present

mix.binary(vcapwide[,c("PSAWeek4", "Group", "CastrationDate")]) 
# Binary coding is only applied to non-numeric fields

Example output

ID003 ID007 ID008 ID009 ID010 ID016 ID018 ID025 ID027 ID031 ID032 ID037 ID040 
    2     2     2     2     2     2     2     2     2     2     2     2     2 
ID045 ID047 ID054 ID056 ID058 ID075 ID078 ID083 ID084 ID087 ID093 ID097 ID100 
    2     2     2     2     2     2     2     2     2     2     2     2     2 
ID101 ID103 ID104 ID113 ID116 ID121 ID125 ID138 ID141 ID144 ID152 ID157 ID158 
    2     2     2     2     2     2     2     2     2     2     2     2     2 
ID160 ID162 ID174 ID182 ID192 ID198 
    2     2     2     2     2     2 
      PSAWeek4 Group_Vehicle Group_MDV Group_ARN CastrationDate_100413
ID003    24.78             1         0         0                     1
ID007     8.59             0         1         0                     0
ID008    22.85             0         1         0                     1
ID009    26.81             1         0         0                     1
ID010     6.18             0         0         1                     0
ID016    13.19             1         0         0                     0
ID018    12.85             0         1         0                     0
ID025    29.24             0         1         0                     1
ID027    15.83             0         1         0                     1
ID031    19.20             0         0         1                     1
ID032    20.36             1         0         0                     1
ID037    21.01             1         0         0                     1
ID040    32.89             0         0         1                     1
ID045    16.81             1         0         0                     1
ID047     6.07             0         1         0                     0
ID054    11.75             1         0         0                     0
ID056    21.69             0         0         1                     1
ID058    16.98             0         1         0                     1
ID075     5.04             1         0         0                     0
ID078    20.57             0         0         1                     1
ID083     7.89             0         1         0                     0
ID084     4.57             1         0         0                     0
ID087    13.29             0         1         0                     0
ID093    17.89             0         1         0                     1
ID097    27.76             0         1         0                     1
ID100     5.38             0         1         0                     0
ID101    30.03             1         0         0                     1
ID103    11.39             1         0         0                     0
ID104    36.98             0         0         1                     1
ID113    28.73             0         0         1                     1
ID116    29.45             1         0         0                     1
ID121    28.18             0         1         0                     1
ID125    14.00             0         0         1                     0
ID138    16.31             0         1         0                     1
ID141     6.73             0         0         1                     0
ID144    22.94             0         0         1                     1
ID152    17.49             1         0         0                     1
ID157     9.65             1         0         0                     0
ID158    11.10             0         0         1                     0
ID160    28.10             1         0         0                     1
ID162    27.43             0         0         1                     1
ID174    16.60             0         0         1                     1
ID182    12.19             0         0         1                     0
ID192    15.20             0         1         0                     1
ID198    12.06             0         0         1                     0
      CastrationDate_170413
ID003                     0
ID007                     1
ID008                     0
ID009                     0
ID010                     1
ID016                     1
ID018                     1
ID025                     0
ID027                     0
ID031                     0
ID032                     0
ID037                     0
ID040                     0
ID045                     0
ID047                     1
ID054                     1
ID056                     0
ID058                     0
ID075                     1
ID078                     0
ID083                     1
ID084                     1
ID087                     1
ID093                     0
ID097                     0
ID100                     1
ID101                     0
ID103                     1
ID104                     0
ID113                     0
ID116                     0
ID121                     0
ID125                     1
ID138                     0
ID141                     1
ID144                     0
ID152                     0
ID157                     1
ID158                     1
ID160                     0
ID162                     0
ID174                     0
ID182                     1
ID192                     0
ID198                     1

hamlet documentation built on May 1, 2019, 8:40 p.m.