make.dummies: Create Dummy Variables from a Data Frame

Description Usage Arguments Value Author(s) Examples

Description

Create dummy variables using dummy.code from the psych package. The dummy variables' names can be customized and the variables can be added to the input data frame.

Usage

1
2
3
make.dummies(dat, cols, colname.as.prefix = TRUE, delimiter = ".", 
capitalize = FALSE, nchar = NULL, add = TRUE, sort.into.dat = TRUE, 
oneToColname = FALSE, zeroToNA = FALSE, factor.indices = FALSE )

Arguments

dat

A data frame

cols

colnames of variables to be dummy coded

colname.as.prefix

Logical: If TRUE the original variable name is added as prefix

delimiter

A character string by which the variable name and the level name will be separated (only evaluated if colname.as.prefix = TRUE)

capitalize

Logical: If TRUE the level names are capitalized

nchar

Number of characters the level names should be truncated to

add

Logical: If TRUE the dummy variables are appended to dat

sort.into.dat

Logical: If TRUE (and add = TRUE) the dummy variables are added and sorted into dat according to their column names

oneToColname

Logical: If TRUE, the values of cases with a value of 1 on the dummy variable are set to the colname of respective column. This changes the column class of the dummy variable(s) from numeric to character.

zeroToNA

Logical: If TRUE, the values of cases with a value of 0 on the dummy variable are set to NA.

factor.indices

Logical: If TRUE, numeric indices of factor levels are used instead of factor level names.

Value

A data frame with dummy variables. Depending on add the returned object contains either the original data frame with the dummy variables appended or only the dummy variables.

Author(s)

Martin Hecht

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
31
32
## Not run: 
data(science1)

science1.dum <- make.dummies(science1, c("sex","booklet"))
str(science1.dum[,1:12])

science1.dum <- make.dummies(science1, c("sex","booklet"), nchar = 1)
str(science1.dum[,1:12])

science1.dum <- make.dummies(science1, c("sex","booklet"), delimiter = "_")
str(science1.dum[,1:12])

science1.dum <- make.dummies(science1, c("sex","booklet"), delimiter = "", capitalize = TRUE)
str(science1.dum[,1:12])

science1.dum <- make.dummies(science1, c("sex","booklet"), colname.as.prefix = FALSE)
str(science1.dum[,1:12])

science1.dum <- make.dummies(science1, c("sex","booklet"), sort.into.dat = FALSE)
str(science1.dum[ , (ncol(science1.dum)-9):ncol(science1.dum)])

science1.dum <- make.dummies(science1, c("sex","booklet"), add = FALSE)
str(science1.dum)

science1.dum <- make.dummies(science1, c("sex","booklet"), oneToColname = TRUE, zeroToNA = TRUE)
str(science1.dum[,1:12])

science1.dum <- make.dummies(science1, c("sex","booklet"), factor.indices = TRUE)
str(science1.dum[,1:12])


## End(Not run)

eatTools documentation built on May 2, 2019, 4:44 p.m.