3.3 Recoding a variable

You might want to change the shape of a variable. Let’s look at two examples. One is taking a continuous variable and recoding it to be a categorical variable. There are sometimes reasons in doing analysis that we may want to do this. The second is recoding a categorical variable into a new categorical variable.

For this we are going to use another new package, sjmisc. You will need to both install and load this package. Details are in section 1.7 and section 1.8 if you need help.

Note to run the frequency you use the command freq.

3.3.1 Recode a continuous variable to a categorical variable

States and Gapminder

You might want a continuous variable to be in categories for analysis. The example uses the Gapminder dataset. We will divide the mortality rate into two categories: high mortality and low mortality. To ease interpretation, we add labels. We will use the mutate command from the dplyr package and the dicho and split_var commands from the sjMisc package. For each new variable, we will do the following:

  1. Filter data so we have only data for one year.
  2. Create a new variable with the categorical variable.
  3. Add value labels using the ordered command.
  4. After creating the new variable, we will look at the frequency distribution.
library(sjmisc)
my_gapminder_data_2003 <- my_gapminder_data_2003 %>%
  mutate(mortality_rate_dicho = dicho(mortalityRate))

freq(my_gapminder_data_2003$mortality_rate_dicho)
## Frequencies  
## my_gapminder_data_2003$mortality_rate_dicho  
## Type: Factor  
## 
##               Freq   % Valid   % Valid Cum.   % Total   % Total Cum.
## ----------- ------ --------- -------------- --------- --------------
##           0     83     50.92          50.92     50.92          50.92
##           1     80     49.08         100.00     49.08         100.00
##        <NA>      0                               0.00         100.00
##       Total    163    100.00         100.00    100.00         100.00
my_gapminder_data_2003$mortality_rate_dicho <- ordered(my_gapminder_data_2003$mortality_rate_dicho,
                levels = c(0,1),
                labels = c("Low Mortality", "High Mortality"))

freq(my_gapminder_data_2003$mortality_rate_dicho)
## Frequencies  
## my_gapminder_data_2003$mortality_rate_dicho  
## Type: Ordered Factor  
## 
##                        Freq   % Valid   % Valid Cum.   % Total   % Total Cum.
## -------------------- ------ --------- -------------- --------- --------------
##        Low Mortality     83     50.92          50.92     50.92          50.92
##       High Mortality     80     49.08         100.00     49.08         100.00
##                 <NA>      0                               0.00         100.00
##                Total    163    100.00         100.00    100.00         100.00

The dicho command splits the variable in half. The countries with lower mortality rates are labeled “Low Mortality.” The upper half of countries are labeled “High Mortality.”

Below the split_var command splits the variable, the percentage of women in state legislatures in the States dataset, into thirds. We could change n = to a larger number to create more evenly sized groups. [Many more options are here.][https://strengejacke.github.io/sjmisc/articles/recodingvariables.html]

my_usstate_data_2011_small <- my_usstate_data %>% 
  filter(year == 2011 & st != "DC") %>%
  select(state, gini_coef, incshare_top1, pctwomenearn, pctfemaleleg, evangelical_pop, region, povrate)

freq(my_usstate_data_2011_small$pctfemaleleg)
## Frequencies  
## my_usstate_data_2011_small$pctfemaleleg  
## Type: Numeric  
## 
##               Freq   % Valid   % Valid Cum.   % Total   % Total Cum.
## ----------- ------ --------- -------------- --------- --------------
##         9.4      1      2.00           2.00      2.00           2.00
##        12.8      1      2.00           4.00      2.00           4.00
##        13.6      1      2.00           6.00      2.00           6.00
##        14.4      1      2.00           8.00      2.00           8.00
##        14.9      2      4.00          12.00      4.00          12.00
##          16      1      2.00          14.00      2.00          14.00
##        17.3      1      2.00          16.00      2.00          16.00
##        17.4      1      2.00          18.00      2.00          18.00
##        17.9      1      2.00          20.00      2.00          20.00
##        18.2      1      2.00          22.00      2.00          22.00
##        18.8      1      2.00          24.00      2.00          24.00
##        19.3      1      2.00          26.00      2.00          26.00
##          20      1      2.00          28.00      2.00          28.00
##        20.9      1      2.00          30.00      2.00          30.00
##          21      1      2.00          32.00      2.00          32.00
##        21.2      1      2.00          34.00      2.00          34.00
##        21.3      2      4.00          38.00      4.00          38.00
##        22.2      1      2.00          40.00      2.00          40.00
##        22.4      2      4.00          44.00      4.00          44.00
##        22.7      1      2.00          46.00      2.00          46.00
##        23.3      1      2.00          48.00      2.00          48.00
##        23.7      1      2.00          50.00      2.00          50.00
##        23.9      1      2.00          52.00      2.00          52.00
##          24      1      2.00          54.00      2.00          54.00
##        24.5      1      2.00          56.00      2.00          56.00
##        24.8      1      2.00          58.00      2.00          58.00
##        25.6      1      2.00          60.00      2.00          60.00
##        25.7      1      2.00          62.00      2.00          62.00
##        25.8      2      4.00          66.00      4.00          66.00
##        26.8      1      2.00          68.00      2.00          68.00
##        27.3      1      2.00          70.00      2.00          70.00
##        27.6      1      2.00          72.00      2.00          72.00
##        27.8      1      2.00          74.00      2.00          74.00
##        28.3      2      4.00          78.00      4.00          78.00
##        28.6      1      2.00          80.00      2.00          80.00
##          29      1      2.00          82.00      2.00          82.00
##        29.9      1      2.00          84.00      2.00          84.00
##        30.9      1      2.00          86.00      2.00          86.00
##        31.1      1      2.00          88.00      2.00          88.00
##        31.3      1      2.00          90.00      2.00          90.00
##          32      1      2.00          92.00      2.00          92.00
##        34.2      1      2.00          94.00      2.00          94.00
##        34.4      1      2.00          96.00      2.00          96.00
##        38.3      1      2.00          98.00      2.00          98.00
##          41      1      2.00         100.00      2.00         100.00
##        <NA>      0                               0.00         100.00
##       Total     50    100.00         100.00    100.00         100.00
my_usstate_data_2011_small <- my_usstate_data_2011_small %>%
  mutate(pctfemaleleg_3cat = split_var(pctfemaleleg, n = 3)) 

my_usstate_data_2011_small$pctfemaleleg_3cat <- ordered(my_usstate_data_2011_small$pctfemaleleg_3cat,
                     levels = c(1,2, 3),
                     labels = c("Low % Women", "Medium % Women", "High % Women"))

freq(my_usstate_data_2011_small$pctfemaleleg_3cat)
## Frequencies  
## my_usstate_data_2011_small$pctfemaleleg_3cat  
## Type: Ordered Factor  
## 
##                        Freq   % Valid   % Valid Cum.   % Total   % Total Cum.
## -------------------- ------ --------- -------------- --------- --------------
##          Low % Women     17     34.00          34.00     34.00          34.00
##       Medium % Women     16     32.00          66.00     32.00          66.00
##         High % Women     17     34.00         100.00     34.00         100.00
##                 <NA>      0                               0.00         100.00
##                Total     50    100.00         100.00    100.00         100.00

3.3.2 Recode a new categorical variable from an existing categorical variable

WVS

Sometimes you may want to recode your variable so that it has fewer categories. For example, look at the question below from the Ethiopia VWS dataset. It asks how democratically is this country being governed today. There are ten categories, ranging from “Not at all democratic” to “Completely Democratic.”

freq(ethiopia_small$Q251)
## Frequencies  
## ethiopia_small$Q251  
## Label: How democratically is this country being governed today  
## Type: Factor  
## 
##                               Freq   % Valid   % Valid Cum.   % Total   % Total Cum.
## --------------------------- ------ --------- -------------- --------- --------------
##       Not at all democratic    218     17.96          17.96     17.72          17.72
##                           2     54      4.45          22.41      4.39          22.11
##                           3     46      3.79          26.19      3.74          25.85
##                           4     73      6.01          32.21      5.93          31.79
##                           5    282     23.23          55.44     22.93          54.72
##                           6    123     10.13          65.57     10.00          64.72
##                           7     94      7.74          73.31      7.64          72.36
##                           8     89      7.33          80.64      7.24          79.59
##                           9     37      3.05          83.69      3.01          82.60
##       Completely democratic    198     16.31         100.00     16.10          98.70
##                        <NA>     16                               1.30         100.00
##                       Total   1230    100.00         100.00    100.00         100.00

This table is hard to interpret. To ease interpretation, we will recode the data into three groups:

  1. For people who answered 1 through 3. We’ll call this “Not Democratic.”
  2. For people who answered 4 through 7. We’ll call this “Somewhat Democratic.”
  3. For people who answered 8 through 10. We’ll call this “Democratic.”

We’ll use the fct_collapse command from forcats, part of the tidyverse here.

Note we gave the new variable a new name—Q251_recode—to distinguish it from Q251. This is good practice. We may want the original variable later!

Look at the frequency of the new variable. It gives us a better picture of how Ethiopians feel about the success of democracy in their country.

ethiopia_small$Q251_recode <- fct_collapse(ethiopia_small$Q251, 
                                   "Not Democratic" = c("Not at all democratic", "2", "3"), 
                                   "Somewhat Democratic" = c("4", "5", "6", "7"),
                                   "Democratic" = c("8", "9", "Completely democratic"))

freq(ethiopia_small$Q251_recode)
## Frequencies  
## ethiopia_small$Q251_recode  
## Label: How democratically is this country being governed today  
## Type: Factor  
## 
##                             Freq   % Valid   % Valid Cum.   % Total   % Total Cum.
## ------------------------- ------ --------- -------------- --------- --------------
##            Not Democratic    318     26.19          26.19     25.85          25.85
##       Somewhat Democratic    572     47.12          73.31     46.50          72.36
##                Democratic    324     26.69         100.00     26.34          98.70
##                      <NA>     16                               1.30         100.00
##                     Total   1230    100.00         100.00    100.00         100.00

A second example For this second example, we will turn a 5-point scale into a 3-point scale.

To simplify things, we will convert this to three responses: agree, neither agree or disagree, and disagree. We use the forcats fct_collapse command.

freq(ethiopia_small$Q34)
## Frequencies  
## ethiopia_small$Q34  
## Label: Jobs scarce: Employers should give priority to (nation) people than immigrants  
## Type: Factor  
## 
##                                    Freq   % Valid   % Valid Cum.   % Total   % Total Cum.
## -------------------------------- ------ --------- -------------- --------- --------------
##                   Agree strongly    541     44.09          44.09     43.98          43.98
##                            Agree    372     30.32          74.41     30.24          74.23
##       Neither agree nor disagree     43      3.50          77.91      3.50          77.72
##                         Disagree    206     16.79          94.70     16.75          94.47
##                Disagree strongly     65      5.30         100.00      5.28          99.76
##                             <NA>      3                               0.24         100.00
##                            Total   1230    100.00         100.00    100.00         100.00
ethiopia_small$Q34_recode <- fct_collapse(ethiopia_small$Q34,
                                           "Agree" = c("Agree strongly", "Agree"),
                                           "Neither agree nor disagree" = c("Neither agree nor disagree"),
                                           "Disagree" = c("Disagree", "Disagree strongly"))

freq(ethiopia_small$Q34_recode)
## Frequencies  
## ethiopia_small$Q34_recode  
## Label: Jobs scarce: Employers should give priority to (nation) people than immigrants  
## Type: Factor  
## 
##                                    Freq   % Valid   % Valid Cum.   % Total   % Total Cum.
## -------------------------------- ------ --------- -------------- --------- --------------
##                            Agree    913     74.41          74.41     74.23          74.23
##       Neither agree nor disagree     43      3.50          77.91      3.50          77.72
##                         Disagree    271     22.09         100.00     22.03          99.76
##                             <NA>      3                               0.24         100.00
##                            Total   1230    100.00         100.00    100.00         100.00