Ben - 7 months ago 31

R Question

Possibly a dumb question, but I recently saw Matt Dowle write some code with

`as.factor()`

`for (col in names_factors) set(dt, j=col, value=as.factor(dt[[col]]))`

`as.factor(dt[[col]])`

`factor(dt[[col]], levels=my_levels)`

`as.factor()`

`factor()`

Answer

`as.factor`

is a wrapper for `factor`

, but it allows quick return if the input vector is already a factor:

```
function (x)
{
if (is.factor(x))
x
else if (!is.object(x) && is.integer(x)) {
levels <- sort(unique.default(x))
f <- match(x, levels)
levels(f) <- as.character(levels)
if (!is.null(nx <- names(x)))
names(f) <- nx
class(f) <- "factor"
f
}
else factor(x)
}
```

Per @Frank: Stating the obvious here, but: it's not a mere wrapper, since this "quick return" will leave factor levels and ordered-ness, alone while `factor()`

will not:

```
f = factor("a", levels=c("a","b"))
#[1] a
#Levels: a b
factor(f)
#[1] a
#Levels: a
as.factor(f)
#[1] a
#Levels: a b
```