Chapter 6 Recoding data: invert

6.1 Intro

A common recoding task is inverting an item. For example, if scores used to be on a 1-5 scale, after recoding, 1 has become 5, 2 became 4, 4 became 2, and 5 became 1.

There are generally two ways of achieving this:

  • subtracting all values from the maximum value minus one (so to recode values on a 1-5 scale, subtract all values from 6 to obtain the recoded values)
  • specifying, for each value, the new value you want

In this chapter, both methods will be listed where appropriate. Note that when creating new variable names, it is important to follow the convention for variable names (see section (software-basics-file-and-variable-name-conventions)).

6.1.1 Example dataset

This example uses the Rosetta Stats example dataset “pp15” (see Chapter 1 for information about the datasets and Chapter 3 for an explanation of how to load datasets).

6.1.2 Variable(s)

From this dataset, this example uses variable highDose_IntentionRAA_intention, a variable with these data:

1 2 3 4 5 6 7
64 37 31 53 44 47 32

We want to invert these, such that after recoding, in the new variable 64 participants have value 7, 37 participants have value 6, 31 participants have value 5, 53 participants have value 4, 44 participants have value 3, 47 participants have value 2, and 32 participants have value 1.

6.2 Input: jamovi

6.2.1 Subtraction

To subtract all values from the maximum value (in this case, 8), use the following steps.

In the “Data” tab, click the “Compute” button as shown in Figure 6.1.

Recoding in jamovi: opening Compute menu

Figure 6.1: Recoding in jamovi: opening Compute menu

The, type in the new variable name in the top-most text field labelled “COMPUTED VARIABLE”, and in the bottom-right text field, specify the value from which you want to subtract all variable values (in this case, 8), a minus (the dash), and the source variable name (in this example, highDose_IntentionRAA_intention), as shown in Figure 6.2.

Recoding in jamovi: specifying Compute function

Figure 6.2: Recoding in jamovi: specifying Compute function

6.2.2 Manual specification

To manually specify each value you want, use the following steps.

In the “Data” tab, click the “Transform” button as shown in Figure 6.3.

Recoding in jamovi: opening Transform menu

Figure 6.3: Recoding in jamovi: opening Transform menu

In the dropdown labelled “Source variable”, select the variable to transform as shown in Figure 6.4.

Recoding in jamovi: selecting the variable to transform

Figure 6.4: Recoding in jamovi: selecting the variable to transform

In the text field labelled “TRANSFORMED VARIABLE” at the rop, specify the new variable name as shown in Figure 6.5.

Recoding in jamovi: specifying the new variable name

Figure 6.5: Recoding in jamovi: specifying the new variable name

In the dropdown labelled “using transform”, select “Create New Transform…” as shown in Figure 6.6.

Recoding in jamovi: creating a new transformation

Figure 6.6: Recoding in jamovi: creating a new transformation

In the top-most text field, just below the text “TRANSFORM”, you can name this transformation in case you plan do you can easily re-use it for other variables. Then, click the “Add recode condition” to add a new recoding condition as shown in Figure 6.7.

Recoding in jamovi: naming a transformation

Figure 6.7: Recoding in jamovi: naming a transformation

Keep repeating this as many times as you have values that you want to recode, so that you get a list of recode conditions as shown in Figure 6.8.

Recoding in jamovi: adding recode conditions

Figure 6.8: Recoding in jamovi: adding recode conditions

Then, for each recoding condition, specify the condition between “$source” amd “use”, and specify the value you want to assign if that condition is true after “use” as shown in Figure 6.9.

Recoding in jamovi: specifying the recodes

Figure 6.9: Recoding in jamovi: specifying the recodes

On the last row, after “else use”, you can specify which value to assign in rows where the source variable does not match any of the conditions. Because jamovi immediately shows every change in the data spreadsheet you see at the bottom, it is easy to play around until you get it right.

6.3 Input: R

6.3.1 Subtraction

To subtract all values from the maximum value (in this case, 8), you can use the following command (note that this requires the example dataset to be stored under name dat, see section 3):

dat$highDose_IntentionRAA_intention_recoded <-
  8 - dat$highDose_IntentionRAA_intention;

6.3.2 Manual specification

To manually specify each value you want, you can use the following command (this requires the rosetta package to be installed, see section 2.3.2, and the example dataset to be stored under name dat, see section 3):

dat$highDose_IntentionRAA_intention_recoded <-
  rosetta::recode(
    dat$highDose_IntentionRAA_intention,
    "1=7; 2=6; 3=5; 4=4; 5=3; 6=2; 7=1"
  );

6.4 Input: SPSS GUI

First activate the dat dataset (see 2.4.1).

A screenshot placeholder

Figure 6.10: A screenshot placeholder

6.5 Input: SPSS Syntax

6.5.1 Subtraction

To subtract all values from the maximum value (in this case, 8), you can use the following command (this requires the dat dataset to be the active dataset, see 2.4.1):

COMPUTE highDose_IntentionRAA_intention_recoded =
  8 - highDose_IntentionRAA_intention.

6.5.2 Manual specification

To manually specify each value you want, you can use the following command:

RECODE highDose_IntentionRAA_intention
  (1=7) (2=6) (3=5) (4=4) (5=3) (6=2) (7=1)
  INTO highDose_IntentionRAA_intention_recoded.

6.6 Output

Recoding a variable is not an analysis, and as such, does not produce output. You can inspect the newly created variable to ensure it has been created properly.