Recode/relevel data.frame factors with different levels

aL3xa picture aL3xa · Feb 26, 2010 · Viewed 12k times · Source

Each time when I have to recode some set of variables, I have SPSS recode function in mind. I must admit that it's quite straightforward. There's a similar recode function in car package, and it does the trick, but let's presuppose that I want to get things done with factor.

I have data.frame with several variables with value range from 1 to 7. I want to "reverse" variable values, hence replacing 1s with 7s, 2s with 6s, 3s with 5s etc. I can utilize factor function:

# create dummy factor
set.seed(100)
x <- as.factor(round(runif(100,1,7)))
y <- factor(x, levels = rev(levels(x)))

And if I run:

> levels(x)
[1] "1" "2" "3" "4" "5" "6" "7"
> levels(y)
[1] "7" "6" "5" "4" "3" "2" "1"

Problem starts when I want to recode factors that do not have equal levels. If some factor, z, has levels c("1", "3", "4", "6", "7"), is there any chance that I can "reverse" levels so 1=7, 2=6, 3=5 etc. by utilizing factor function?

Other efficient recode functions should suffice!

Answer

Marek picture Marek · Feb 26, 2010

You must provide levels argument to factor (as Dirk wrote):

set.seed(2342472)
( x <- round(runif(10,1,7)) )
#  [1] 7 5 5 3 1 2 5 3 3 2
( xf <- as.factor(x) )
# [1] 7 5 5 3 1 2 5 3 3 2
# Levels: 1 2 3 5 7
( yf <- factor(x,levels=7:1) )
# [1] 7 5 5 3 1 2 5 3 3 2
# Levels: 7 6 5 4 3 2 1

you could do this on existing factor too

( yxf <- factor(xf,levels=7:1) )
# [1] 7 5 5 3 1 2 5 3 3 2
#Levels: 7 6 5 4 3 2 1

As you see levels were extended in desire order.