Tuesday, January 3, 2012

Reverse coding variables in R

Recoding variables in R can be a bit more difficult than in SPSS.


A lot of recoding in psychology involves recoding items into the other direction (reverse coding). Especially when you need to recode multiple items, with many values, the following example may be useful. It involves recoding the four positively worded items of the CES-D (4, 8, 12 and 16). In this case, instead of the usual 4 response categories, respondents were asked how many days in the past week the statement had applied to them.

This line would be all you need (see fully working example below):
CESD[,c(4,8,12,16)] <- 7 - CESD[,c(4,8,12,16)]

You take (highest possible response category code + lowest possible response category code) - the original coded variables

The result is the recoded variable. The items at hand were answered on an 8 point rating scale, ranging from 0-7 (days). If you have a 5 point Likert scale, ranging from 1-5, use '6 - .'. If you have a 4 point Likert scale, ranging from 0-3, use '3 - .'.

If you have to recode the levels of an (ordered or nominal) factor, use the x1 and x2 examples below.

R code

 
x1 <- factor(rep(1:3, times = 3))
x1
levels(x1) <- c("not at all", "a little", "very much")
x1
levels(x1) <- levels(x)[3:1]
x1

x2 <- factor(rep(1:5, times = 3))
x2
levels(x2) <- levels(factor(5:1))
x2

CESD <- matrix(c(0, 1, 0, 7, 1, 0, 7, 0, 0, 0, 0, 0, 0, 7, 4, 0, 0, 2, 0, 1,
1, 0, 0, 7, 2, 1, 7, 0, 2, 1, 1, 2, 0, 7, 1, 5, 2, 1, 1, 1,
1, 0, 0, 7, 3, 1, 7, 0, 2, 0, 0, 0, 7, 2, 0, 5, 0, 4, 2, 0,
0, 0, 0, 4, 1, 1, 5, 0, 1, 0, 0, 0, 0, 5, 0, 5, 0, 1, 0, 0,
2, 1, 0, 2, 0, 6, 2, 7, 0, 6, 1, 1, 1, 5, 3, 5, 1, 7, 2, 2,
0, 1, 0, 7, 0, 0, 7, 0, 1, 1, 0, 1, 0, 7, 1, 7, 1, 1, 0,NA,
0, 0, 0, 6, 1, 0, 7, 0, 0, 0, 0, 0, 0, 7, 0, 7, 2, 4, 1, 0,
0, 0, 0, 7, 1, 0, 7, 0, 0, 0, 0, 0, 0, 7, 0, 7, 0, 0, 0, 0,
0, 0, 1, 3, 0, 1, 5, 0, 0, 1, 1, 0, 0, 7, 2, 4, 0, 0, 0, 1,
7, 7, 7, 0, 0, 7, 0, 7, 0, 7, 7, 7, 4, 7, 7, 4, 7, 7, 4, 0), ncol=20)

CESD[,c(4,8,12,16)] # to check if everything went well, later on
CESD[,c(4,8,12,16)] <- abs(CESD[,c(4,8,12,16)] - 7) # the actual recode
CESD[,c(4,8,12,16)] # compare with above: everything went well!

4 comments:

  1. You can leave out the abs and simplify the code by using:

    CESD[,c(4,8,12,16)] <- 7-(CESD[,c(4,8,12,16)])

    ReplyDelete
  2. thanks for this very helpful post! I've added to it by breaking it down:

    http://psycnotes.wikispaces.com/R+Notes

    ReplyDelete
  3. http://psycnotes.wordpress.com/how-to-recode-in-r/

    ReplyDelete