Reverse scoring using R

Reverse Scoring using R Statistical Environment

Spread the love

This post is my first on R and it will describe a method on how to reverse scores using R.

Reverse scoring in R

Many instruments (i.e., questionnaires) contain items are phrased so that a strong agreement indicates something negative (e.g., “When there is music in the room I find it hard to concentrate on reading”). These items need to be reversed so that the data will be correct later for statistical analysis.

For more information on reverse scoring, please see my earlier post: Reverse scoring in Python.  Since I was more  familiar with Python  compared to R, and I had no a clue on how to do this in SPSS, I wrote a Python script. The Python script used a function that used Pandas DataFrame and it reversed the scores nice and quickly.

Questionnaire data to be reversed

However, in R was pretty much as simple to do reverse scoring as in Python. In the following script a data frame is generated with column names (i.e., columnNames,’Q1′ to ‘Q6’) and some data is generated using replicate and sample (100 responses, on the 6 questions).

After that you will find two methods, that are pretty much the same. More specifically, the methods to reverse code, only differ in how the columns are selected. In the first reverse scoring method, items are selected based on the index of the column. The second recode method, variables are selected based on the column names (might be preferable if you know the names of columns but not the indices).

Importing Data in R

In R, there are numerous ways to load data into a dataframe object. In the example below, we are creating a fake dataframe but, of course, most of the time we have the data stored on a computer harddrive.

R Script for Reverse Scoring

How do you reverse scores in R? First, create a vector in R, containing the column names, that is to be reeversed:

#Generate column names for the DataFrame (Question 1; Q1, and so on) 
columnNames =c('Q1', 'Q2', 'Q3', 'Q4', 'Q5', 'Q6')

#Generating data for the DataFrame
scores < - as.data.frame(replicate(6, replicate(100, sample(1:5,1))))
colnames(scores) <- columnNames

#Reversing scores in columns 'Q2', 'Q3', and 'Q4'
reversed.scores <- scores
reversed.scores[,c(2,3,4)] <- 6-scores[,c(2,3,4)]

#Alternative, the same as above but based on the column names
columnsToReverse <- c('Q2', 'Q3', 'Q4')
reversed.scores[,columnsToReverse] <- 6-scores[,columnsToReverse]

Reverse Code in R using the Psych Package

In this section, of the reverse coding tutorial, you will learn how to use the psych package when reversing the items.

Working with the reverse.code Function

First, you need to install the r-package called “psych” and then you are going to use the reverse.code function to switch the coding of some items.

install.packages("psych")

Reverse-Code Variables in R using reverse.code

Finally, you can use the psych package to reverse the items. First, you import the package, create a numeric vector (keys), and then we use the reverse.code function. We created the numeric vector because, the reverse.code function, uses these indexes to know which columns to reverse (1st, 3rd, and 4th).

require(psych)


#Reversing scores in columns 'Q2', 'Q3', and 'Q4'
keys < - c(1, -1, -1, -1, 1, 1)

new <- reverse.code(keys, df)
df[1:3,]
new[1:3,]

R and Reproducible Code

Although, the code in this post can be found in this R Jupyter Notebook, this may not be the most optimal way to share the R script including analysis and visualization. This is, because, with time R and it’s packages get updated, sometimes a function may change name, and so on. Thus. it may take time for another researcher to, fully, reproduce the R computaional environment that we used in a study.

Luckily, there are tools such as Binder and Code Ocean to help us with this. See the tutorial on how to use Binder, R, and RStudio for reproducible research. That way, other researchers can run the R script, such as how we reversed the scores here, and (hopefully) get the exact same results.


Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: