red-swan
red-swan

Reputation: 1267

Is there a one-liner to create a factor from a numeric vector of level indices?

I've often had to take a vector of group indicators and wanted to create a factor out of it to explore the data more easily. I've always done this by instantiating the factor and then assigning the levels to it where group indicators are the indices of the levels (perhaps easier to see below). But seeing as factors are the least understood data type for me, I wonder if there is a simple function that will do it all for me that I'm not aware of.

# set seed so we're all on the same page
set.seed(1337)
# create the contrived vector of indices
myNumbers <- sample(x = 1:26, size = 50, replace = TRUE)

# This is how I would create the factor
myFactor <- factor(myNumbers)          # step 1
levels(myFactor) <- letters            # step 2

# Inspect the result
myFactor

Upvotes: 1

Views: 52

Answers (1)

d.b
d.b

Reputation: 32548

You can specify levels when creating the factor from a vector.

foo = factor(x = letters[myNumbers], levels = letters)
length(levels(foo))
#[1] 26

If you don't specify levels when creating factor, one will be automatically assigned from the unique values of vector

length(levels(myFactor))  #before step 2
#[1] 21

It means that, before step 2, the numeric values of factors in myFactor ranges from 1 to 21 (range(as.numeric(myFactor))). As a result, even though you intended to use indices from 1:26, you will be using indices from 1:21.

Upvotes: 3

Related Questions