{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Reverse Scoring Items in a Questionnaire\n", "In this notebook we will learn another methods for reverse coding of questionnaire items using the R statistical programming environment. Here you will learn reverse coding using a function from the psych package in R. This notebook is for the blog post [https://www.marsja.se/reverse-scoring-using-r/](https://www.marsja.se/reverse-scoring-using-r/) in which you will learn more about this." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Data to Reverse\n", "As example data, with some columns to reverse, we create a dataframe:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
Q1Q2Q3Q4Q5Q6
314245
213311
341354
134211
511241
255411
\n" ], "text/latex": [ "\\begin{tabular}{r|llllll}\n", " Q1 & Q2 & Q3 & Q4 & Q5 & Q6\\\\\n", "\\hline\n", "\t 3 & 1 & 4 & 2 & 4 & 5\\\\\n", "\t 2 & 1 & 3 & 3 & 1 & 1\\\\\n", "\t 3 & 4 & 1 & 3 & 5 & 4\\\\\n", "\t 1 & 3 & 4 & 2 & 1 & 1\\\\\n", "\t 5 & 1 & 1 & 2 & 4 & 1\\\\\n", "\t 2 & 5 & 5 & 4 & 1 & 1\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "| Q1 | Q2 | Q3 | Q4 | Q5 | Q6 |\n", "|---|---|---|---|---|---|\n", "| 3 | 1 | 4 | 2 | 4 | 5 |\n", "| 2 | 1 | 3 | 3 | 1 | 1 |\n", "| 3 | 4 | 1 | 3 | 5 | 4 |\n", "| 1 | 3 | 4 | 2 | 1 | 1 |\n", "| 5 | 1 | 1 | 2 | 4 | 1 |\n", "| 2 | 5 | 5 | 4 | 1 | 1 |\n", "\n" ], "text/plain": [ " Q1 Q2 Q3 Q4 Q5 Q6\n", "1 3 1 4 2 4 5 \n", "2 2 1 3 3 1 1 \n", "3 3 4 1 3 5 4 \n", "4 1 3 4 2 1 1 \n", "5 5 1 1 2 4 1 \n", "6 2 5 5 4 1 1 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df <- as.data.frame(replicate(6, replicate(100, sample(1:5,1))))\n", "\n", "colnames(df) <- c('Q1', 'Q2', 'Q3', 'Q4', 'Q5', 'Q6')\n", "\n", "head(df)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Working with the reverse.code function\n", "First, we are going to install the r-package called \"psych\" and then we are going to sue the reverse.code function to switch the coding of some items." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Installing package into 'C:/Users/erima96/Documents/R/win-library/3.6'\n", "(as 'lib' is unspecified)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "package 'psych' successfully unpacked and MD5 sums checked\n", "\n", "The downloaded binary packages are in\n", "\tC:\\Users\\erima96\\AppData\\Local\\Temp\\Rtmpea2YXQ\\downloaded_packages\n" ] } ], "source": [ "install.packages(\"psych\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reverse-Code Variables in R using reverse.code\n", "Now, we're ready to reverse the items using R:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Loading required package: psych\n", "Warning message:\n", "\"package 'psych' was built under R version 3.6.1\"" ] }, { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
Q1Q2Q3Q4Q5Q6
314245
213311
341354
\n" ], "text/latex": [ "\\begin{tabular}{r|llllll}\n", " Q1 & Q2 & Q3 & Q4 & Q5 & Q6\\\\\n", "\\hline\n", "\t 3 & 1 & 4 & 2 & 4 & 5\\\\\n", "\t 2 & 1 & 3 & 3 & 1 & 1\\\\\n", "\t 3 & 4 & 1 & 3 & 5 & 4\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "| Q1 | Q2 | Q3 | Q4 | Q5 | Q6 |\n", "|---|---|---|---|---|---|\n", "| 3 | 1 | 4 | 2 | 4 | 5 |\n", "| 2 | 1 | 3 | 3 | 1 | 1 |\n", "| 3 | 4 | 1 | 3 | 5 | 4 |\n", "\n" ], "text/plain": [ " Q1 Q2 Q3 Q4 Q5 Q6\n", "1 3 1 4 2 4 5 \n", "2 2 1 3 3 1 1 \n", "3 3 4 1 3 5 4 " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
Q1Q2-Q3-Q4-Q5Q6
352445
253311
325354
\n" ], "text/latex": [ "\\begin{tabular}{llllll}\n", " Q1 & Q2- & Q3- & Q4- & Q5 & Q6\\\\\n", "\\hline\n", "\t 3 & 5 & 2 & 4 & 4 & 5\\\\\n", "\t 2 & 5 & 3 & 3 & 1 & 1\\\\\n", "\t 3 & 2 & 5 & 3 & 5 & 4\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "| Q1 | Q2- | Q3- | Q4- | Q5 | Q6 |\n", "|---|---|---|---|---|---|\n", "| 3 | 5 | 2 | 4 | 4 | 5 |\n", "| 2 | 5 | 3 | 3 | 1 | 1 |\n", "| 3 | 2 | 5 | 3 | 5 | 4 |\n", "\n" ], "text/plain": [ " Q1 Q2- Q3- Q4- Q5 Q6\n", "[1,] 3 5 2 4 4 5 \n", "[2,] 2 5 3 3 1 1 \n", "[3,] 3 2 5 3 5 4 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "require(psych)\n", "\n", "\n", "#Reversing scores in columns 'Q2', 'Q3', and 'Q4'\n", "keys <- c(1, -1, -1, -1, 1, 1)\n", "\n", "new <- reverse.code(keys, df)\n", "df[1:3,]\n", "new[1:3,]" ] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "3.6.0" } }, "nbformat": 4, "nbformat_minor": 2 }