Learn R Programming

CARRoT (version 3.0.2)

make_numeric: Turning a non-numeric variable into a numeric one

Description

Function which turns a single categorical (non-numeric) variable into a numeric one (or several) by introducing dummy '0'/'1' variables.

Usage

make_numeric(vari, outcome, ra,mode)

Value

Returned value is an M x N matrix where M is the length of the input array of indices ra and N is length(vari)-1.

Arguments

vari

array of values to be transformed

outcome

TRUE/FALSE indicates whether the variable vari is an outcome (TRUE) or a predictor (FALSE)

ra

indices of the input array vari which indicate which values will be transformed

mode

'binary' (logistic regression), 'multin' (multinomial regression)

Details

This function is essentially a standard way to turn categorical non-numeric variables into numeric ones in order to run a regression

Examples

Run this code
#creating a non-numeric set

a<-t(rmultinom(100,1,c(0.2,0.3,0.5)))%*%c(1,2,3)

a[a==1]='red'
a[a==2]='green'
a[a==3]='blue'

#running the function

make_numeric(a,FALSE,sample(1:100,50),"linear")

make_numeric(a,TRUE,sample(1:100,50))

Run the code above in your browser using DataLab