Anyway to convert ordinal categories to numerical values?

Hi,

I think there must be a way to do this efficiently. I am looking to convert X = {none, mild, moderate, severe} into Y = {0,1,2,3,4} using Tidyverse.

An example:

X

moderate
moderate
severe
none

Which would turn into the following

Y

3
3
4
0

Thank you.

You can use the recode function for this:

library(tidyverse)

set.seed(2)
x = sample(c("none","mild","moderate","severe"), 100, replace=TRUE)

x_numeric = recode(x, none=0, mild=1, moderate=2, severe=3)

Bear in mind that recoding the values as numeric implicitly assumes that the "distance" between each category is constant.

1 Like

Thank you, works like a charm!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.