powered by
In psychology variables often need to be recoded into the other direction (reverse coding). Let's say we have a Likert scale from 1 to 5 and we want to recode the variable so that a 5 becomes a 1, 4 a 2 and so on.
RevCode(x, lbound = min(x, na.rm = TRUE), ubound = max(x, na.rm = TRUE))
a numerical vector, will be converted to numeric if it's not.
the low bound of the variable, default is the minmal value of x.
x
the high bound of the variable, default is the maximal value of x.
the recoded vector
The function recodes based on:
lbound + ubound - x
Recode
# NOT RUN { s <- c(3,4,2,7,4,9,NA,10) RevCode(s, lbound=1, ubound=10) i <- c(1,0,0,0,1,1) cbind(i, RevCode(i)) # }
Run the code above in your browser using DataLab