charToFact: Convert Character Vector to Factor

Description Usage Arguments Value Examples

View source: R/call.R

Description

Similar to base::as.factor but much faster and only for converting character vector to factor.

Usage

1
charToFact(x, decreasing=FALSE, nThread=getOption("kit.nThread"))

Arguments

x

A vectorof type character

decreasing

A boolean. Whether to order levels in decreasing order or not. Default is FALSE.

nThread

Number of thread to use.

Value

The character vector input as a factor. Please note that, unlike as.factor, NA levels are preserved.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
x = c("b","A","B","a","\xe4","a")
Encoding(x) = "latin1"
identical(charToFact(x), as.factor(x))
identical(charToFact(c("a","b",NA,"a")), addNA(as.factor(c("a","b",NA,"a"))))

# Benchmarks
# ----------
# x = sample(letters,3e7,TRUE)
# microbenchmark::microbenchmark(
#   kit=kit::charToFact(x,nThread = 1L),
#   base=as.factor(x),
#   times = 5L
# )
# Unit: milliseconds
# expr  min   lq   mean  median   uq  max neval
# kit   188  190    196     194  200  208     5
# base 1402 1403   1455    1414 1420 1637     5

kit documentation built on March 9, 2021, 5:12 p.m.

Related to charToFact in kit...