krish
krish

Reputation: 1438

Change levels in factor to numbers with different order in R

I have a column in my dataset that would be similar to the factor below:

response = c("Met Expectations", "Exceeded Expectations", "Exceeded    Expectations", "Unacceptable", NA, "Did not meet Expectations" )
factor(response, levels = c("Exceeded Expectations", "Met Expectations", "Did not meet Expectations", "Unacceptable"))

This is what I get for the levels. I have ordered this in the way I get it in the dataset.

[1] Met Expectations          Exceeded Expectations     Exceeded Expectations     Unacceptable             
[5] <NA>                      Did not meet Expectations
Levels: Exceeded Expectations Met Expectations Did not meet Expectations Unacceptable

I want to convert these levels to numbers. So I tried this:

as.numeric(factor(response, levels = c("Exceeded Expectations", "Met Expectations", "Did not meet Expectations", "Unacceptable")))

And I get the correct output:

[1]  2  1  1  4 NA  3

However I want the order to be 4 3 2 1 instead of 1 2 3 4. I am expecting:

Exceeding Expectation to be 4
Met Expectations to be 3
Did not meet Expectations to be 2
Unacceptable to be 1
[1] 3  4  4  1  NA  2

How can I change the order? Is there a straight forward way to do it else I can modify the numbers after converting them to numeric. In this example I can change the order of the levels parameter and get what I want, but since my dataset gives me the levels in this specific order:

Levels: Exceeded Expectations Met Expectations Did not meet Expectations Unacceptable

I was trying figure out a simple way.

Upvotes: 0

Views: 712

Answers (1)

eipi10
eipi10

Reputation: 93761

When you create the factor, change to levels = rev(c("Exceeded Expectations", "Met Expectations", "Did not meet Expectations", "Unacceptable")). rev will reverse the order of the levels, thereby reversing the order of the numerical values assigned to the category labels.

response = c("Met Expectations", "Exceeded Expectations", "Exceeded Expectations", "Unacceptable", NA, "Did not meet Expectations")

response = factor(response, levels = c("Exceeded Expectations", "Met Expectations", "Did not meet Expectations", "Unacceptable"))

as.numeric(response)

#[1]  2  1  1  4 NA  3

response = factor(response, levels = rev(c("Exceeded Expectations", "Met Expectations", "Did not meet Expectations", "Unacceptable")))

as.numeric(response)

#[1]  3  4  4  1 NA  2

If you don't want to change the order of the levels in your factor variable, you can do the following:

response = factor(response, levels = c("Exceeded Expectations", "Met Expectations", "Did not meet Expectations", "Unacceptable"))

# Convert to numeric with numeric order of factor reversed
5 - as.numeric(response)

#[1]  3  4  4  1 NA  2

You can also calculate the number of levels dynamically, in case the number of levels can vary:

# Convert to numeric with numeric order of factor reversed
length(unique(na.omit(response))) + 1 - as.numeric(response)

Upvotes: 2

Related Questions