This post is my first on R and it will describe a method 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.
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, for reversing the can be found. The methods only differ in how the columns are selected. The first are select based on the index of the column and the second select based on the column names to be reversed (might be preferable if you know the names of columns but not the indices).
R Script for Reverse Scoring
#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]
That was pretty simple, right!? Your scores are reversed. I might update this post later with other methods because I am aware of that it might be one or more r-packages that also can reverse scores among other pretty useful things.
The code in this post can be found in this R Jupyter Notebook.