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.
joels
2
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.
Thank you, works like a charm!