Learn R Programming

data.table (version 1.18.2.1)

fctr: Create a factor retaining original ordering

Description

Creates a factor.

By default, the output will have its levels in the original order, i.e., levels = unique(x), as opposed to factor's default where levels = sort(unique(x)).

Usage

fctr(x, levels=unique(x), ..., sort=FALSE, rev=FALSE)

Value

Factor vector having levels ordered according to the order of elements in input and arguments sort, rev.

Arguments

x

Object to be turned into a factor.

levels

Levels for the new factor; unique(x) by default.

...

Other arguments passed to factor.

sort

Logical, default FALSE. Should levels be sorted?

rev

Logical, default FALSE. Should levels be reversed? Applied after sort.

Examples

Run this code
levels(factor(c("b","a","c")))
levels(fctr(c("b","a","c")))
levels(fctr(c("b","a","c"), rev=TRUE))
levels(fctr(c("b","a","c"), sort=TRUE))
levels(fctr(c("b","a","c"), sort=TRUE, rev=TRUE))

Run the code above in your browser using DataLab