0

enter image description here

I have a pid factor variable with 195 levels that I would like to change to a numeric variable per unique patient id. Can anyone assist with that?TIA below is how my data looks like; enter image description here

1 Answers1

0

When you transform the factor to numeric with as.numeric() R doesn't use the labels that you see but the factor levels. If you have the factor as.factor(c(1,2,5,7)) for example, the levels are still 1,2,3,4 as in 1st category, 2nd category, 3rd category & 4th category. The easiest way to use the actual labels is to transform the factor to a character string first and then transform the character string to a numeric variable.

 NewVariable <- as.numeric(as.character(FactorVariable))
Ju Ko
  • 466
  • 7
  • 22