encode_ordinal: Encode Ordinal Data Into Integers

Description Usage Arguments Value Warning See Also Examples

Description

Encodes ordinal data into sequential integers by a given order. Optionally records the result into a log file.

Usage

1
2
encode_ordinal(x, order, none='', out.int=FALSE,
    full_print=TRUE, log = eval.parent(in_log_default))

Arguments

x

The data frame

order

a vector of the ordered labels from low to high.

none

The 'none'-but-not-'NA' level, which is always encoded to 0.

out.int

Whether to convert encoded x to integers. Only set to TRUE when no NA in x because NAs in x causes error when converting to integers. By default, the encoded x is factorial.

full_print

When set to FALSE, only print minimum information. A full output includes summary of x before and after encoding.

log

Controls log files. To produce log files, assign it or the log_arg variable in the parent environment (dynamic scope) a list of arguments for sink(), such as file, append, and split.

Value

An encoded data frame.

Warning

x can only be a data frame. Don't pass a vector to it.

See Also

inspect_map, sink

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# refer to vignettes if you want to use log files
message('refer to vignettes if you want to use log files')

# building a data frame
A <- as.factor(c('y', 'z', 'x', 'y', 'z'))
B <- as.factor(c('y', 'x', 'z', 'z', 'x'))
C <- as.factor(c('k', 'i', 'i', 'j', 'k'))
df <- data.frame(A, B, C)

# encoding
df[, 1:2] <- encode_ordinal(df[,1:2], order = c('z', 'x', 'y'))
df[, 3] <- encode_ordinal(df[, 3, drop = FALSE], order = c('k', 'j', 'i'))
print(df)

Example output

refer to vignettes if you want to use log files
 A     B    
 x:1   x:2  
 y:2   y:1  
 z:2   z:2  
coded 2 cols 3 levels 
 A     B    
 2:1   2:2  
 3:2   3:1  
 1:2   1:2  
 C    
 i:2  
 j:1  
 k:2  
coded 1 cols 3 levels 
 C    
 3:2  
 2:1  
 1:2  
  A B C
1 3 3 1
2 1 2 3
3 2 1 3
4 3 1 2
5 1 2 1

cleandata documentation built on May 1, 2019, 10:25 p.m.