0

So I have a character variable called "Mark" with 3 levels "A", "B", "C" I want to convert it to numerical value for use in linear regression.

When I use as.numeric(Mark), it recodes in increasing alphabetical order A=1, B=2, C=3

But what I want is recode in decreasing alphabet order, like A=3, B=2, C=1

I tried descreasing=TRUE/FALSE, ordered=TRUE/FALSE as options in as.numeric() but it doesn't seem to be working. Is there any easy way of doing this? Thanks for any help

btiger
  • 35
  • 5

1 Answers1

1

You should be able to just re-factor, based on rev(levels(Mark)):

set.seed(10)
x <- factor(sample(LETTERS[1:5], 10, TRUE))
x
#  [1] C B C D A B B B D C
# Levels: A B C D
as.numeric(x)
#  [1] 3 2 3 4 1 2 2 2 4 3
as.numeric(factor(x, levels = rev(levels(x))))
#  [1] 2 3 2 1 4 3 3 3 1 2
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485