charToFact | R Documentation |

Similar to `base::as.factor`

but much faster and only for converting character vector to factor.

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

`x` |
A vectorof type character |

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

`addNA` |
A boolean. Whether to include |

`nThread` |
Number of thread to use. |

The character vector input as a factor. Please note that, unlike `as.factor`

, `NA`

levels are preserved by default, however this can be changed by setting argument `addNA`

to `FALSE`

.

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")))) identical(charToFact(c("a","b",NA,"a"), addNA=FALSE), 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

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.