Social Science Surveys

This chapter introduces the extension package memisc, which is specifically designed to address several of the challenges that were discussed in the previous chapter. It shows how a the system memory can be saved by importing subsets of variables and observations; how variables can be renamed so that results are more easy to interpret; how certain metadata can be used, that are not provided for by a basic R installation, such as value labels and user-defined missing values. The chapter also provides examples for more complex recodings of variables, e.g. for the construction of Goldthorpe class categories for households from ISCO-coded occupations of survey respondents and the creation of codebooks.

Below is the supporting material for the various sections of the chapter.

Importing survey data

Importing data from the British Election Study in SPSS format

  • Script file: importing-BES-data.R

    Required data file: 83BES.sav which is available from https://www.britishelectionstudy.com/data-object/1983-bes-cross-section/ (Note that the file available from this link has filename extension .sav, but [last time I checked] is actually has an SPSS “portable” format, so that the filename extension .por would have appeared more appropriate.)

    The script makes use of the memisc package, which is available from https://cran.r-project.org/package=memisc

  • Interactive notebook:

    In [1]:
    options(jupyter.rich_display=FALSE) # Create output as usual in R
    

    In order to run this notebook successfully, you have to download the 1983 British Election Study from the BES website and upload it to the virtual machine on which this notebook runs. To do this,

    1. pull down the "File" menu item and select "Open"
    2. An overview of the folder that contains the notebook opens.
    3. The folder view has a button labelled "Upload". Use this to upload the file that you downloaded from the BES website. Its name should be 83BES.sav.

    Note that the uploaded data will disappear, once you "Quit" the notebook (and the Jupyter instance).

    In [2]:
    library(memisc) # The functions used here are in this package.
    
    Loading required package: lattice
    
    Loading required package: MASS
    
    
    Attaching package: ‘memisc’
    
    
    The following objects are masked from ‘package:stats’:
    
        contr.sum, contr.treatment, contrasts
    
    
    The following object is masked from ‘package:base’:
    
        as.array
    
    
    
    In [3]:
    # The first step: Optainin the location and description of the survey data file
    BES.1983.por <- spss.portable.file("83BES.sav")
    
    In [4]:
    description(BES.1983.por[1:30])
    
     ID1      'SERIAL NUMBER'                           
     RECORD1  'CARD NUMBER'                             
     AREANO   'AREA NUMBER'                             
     PANO     'PA CONSTITUENCY NUMBER'                  
     MOVER    'MOVER-REISSUE CODE'                      
     INT      'INTERVIEWER NUMBER'                      
     FGLCLASS 'FATHER'S GOLDTHORPE-LLEWELLYN CLASS'     
     RGLCLASS 'RESPONDENT'S GOLDTHORPE-LLEWELLYN CLASS' 
     SGLCLASS 'SPOUSE'S GOLDTHORPE-LLEWELLYN CLASS'     
     FGHCLASS 'FATHER'S GOLDTHORPE-HEATH CLASS'         
     RGHCLASS 'RESPONDENT'S GOLDTHORPE-HEATH CLASS'     
     SGHCLASS 'SPOUSE'S GOLDTHORPE-HEATH CLASS'         
     Q1       'CARE WHICH PARTY WON'                    
     Q2A      'FOLLOWED ELECTION BROADCASTS'            
     Q2B      'READ ABOUT CAMPAIGN IN NEWSPAPER'        
     Q2C      '[IF READ NEWSPP] WHICH DAILY ON CAMPAIGN'
     Q2D      'HEARD CANDIDATE AT POLITICAL MEETING'    
     Q2E      'CANVASSED FOR CANDIDATE'                 
     Q2F      'PUT UP POLITICAL PARTY POSTER'           
     Q2G      'CANVASSER CALLED DURING CAMPAIGN'        
     Q2H1     'CON CANVASSER CALLED'                    
     Q2H2     'LAB CANVASSER CALLED'                    
     Q2H3     'ALLIANCE CANVASSER CALLED'               
     Q2H4     'LIB CANVASSER CALLED'                    
     Q2H5     'SOCIAL DEMOCRACTIC CANVASSER CALLED'     
     Q2H6     'SNP CANVASSER CALLED'                    
     Q2H7     'PLAID CYMRU CANVASSER CALLED'            
     Q2H8     'OTHER PARTY CANVASSER CALLED'            
     Q2H9     'ECOLOGY PARTY CANVASSER CALLED'          
     Q2H10    'NATIONAL FRONT CANVASSER CALLED'         
    
    In [5]:
    # The second step: Loading a subset of the data - only the variables that are
    # really needed for the analysis.
    BES.1983.classvot <- subset(
        BES.1983.por,
        select=c(
            voted          = Q7A,
            vote           = Q9A,
            lrself         = Q46G,
            fglclass       = FGLCLASS,
            rglclass       = RGLCLASS,
            sglclass       = SGLCLASS,
            ethnicity      = Q64A,
            gender         = Q64B,
            age            = Q56,
            educ           = Q59A,
            religion       = Q63A,
            religatt       = Q63B
        ))
    description(BES.1983.classvot)
    
     voted     'DID RESPONDENT VOTE 1983'               
     vote      '[IF VOTED] PARTY VOTED FOR'             
     lrself    'LEFT,RIGHT SCALE: R'S POSITION 1983'    
     fglclass  'FATHER'S GOLDTHORPE-LLEWELLYN CLASS'    
     rglclass  'RESPONDENT'S GOLDTHORPE-LLEWELLYN CLASS'
     sglclass  'SPOUSE'S GOLDTHORPE-LLEWELLYN CLASS'    
     ethnicity 'RESPONDENT'S ETHNIC ORIGIN'             
     gender    'RESPONDENT'S SEX'                       
     age       'RESPONDENT'S AGE LAST BIRTHDAY'         
     educ      'AGE RESPONDENT LEFT SCHOOL'             
     religion  'RESPONDENT'S RELIGION'                  
     religatt  '[IF RELIGION] FREQUENCY OF ATTENDANCE'  
    
    In [6]:
    # Declaring the measurement level of a single variable
    measurement(BES.1983.classvot$lrself) <- "interval"
    
    In [7]:
    # Declaring the measurement level of several variables
    BES.1983.classvot <- within(BES.1983.classvot,{
       measurement(lrself) <- "interval"
       measurement(age) <- "interval"
       measurement(educ) <- "interval"
    })
    
    In [8]:
    # Declaring the measurement level of several variables using a loop
    BES.1983.classvot <- within(BES.1983.classvot,{
       foreach(var=c(lrself,age,educ),{
               measurement(var) <- "interval"
       })
    })
    
    In [9]:
    # Obtaining the codebook a single variable
    codebook(BES.1983.classvot["age"])
    
    ================================================================================
    
       age 'RESPONDENT'S AGE LAST BIRTHDAY'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: interval
    
       Values and labels       N Percent
                                        
       99 'NA'                20     0.5
                                        
            Min: 18.000                 
            Max: 99.000                 
           Mean: 45.704                 
       Std.Dev.: 18.174                 
    
    In [10]:
    # Declaring the missing values for this variable
    missing.values(BES.1983.classvot$age) <- 99
    
    In [11]:
    # Declaring several missing values
    BES.1983.classvot <- within(BES.1983.classvot,{
        missing.values(voted)     <- 9
        missing.values(vote)      <- 95:99
        missing.values(lrself)    <- 95:99
        missing.values(fglclass)  <- 0
        missing.values(rglclass)  <- 0
        missing.values(sglclass)  <- 0
        missing.values(ethnicity) <- 9
        missing.values(gender)    <- 9
        missing.values(age)       <- 9
        missing.values(age)       <- 98:99
        missing.values(religion)  <- 99
        missing.values(religatt)  <- 7:9
    })
    
    In [12]:
    # The codebook of the resulting data set object
    codebook(BES.1983.classvot)
    
    ================================================================================
    
       voted 'DID RESPONDENT VOTE 1983'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: 9
    
       Values and labels       N Percent
                                        
       1   'YES'            3295    83.3
       2   'NO'              660    16.7
    
    ================================================================================
    
       vote '[IF VOTED] PARTY VOTED FOR'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: 95, 96, 97, 98, 99
    
       Values and labels         N Valid Total
                                              
        0   'SKIPPED'          660  17.1  16.7
        1   'CON'             1432  37.1  36.2
        2   'LAB'              937  24.3  23.7
        3   'ALLIANCE'         416  10.8  10.5
        4   'LIB'              278   7.2   7.0
        5   'SOCIAL  DEMOCR'    94   2.4   2.4
        6   'SNP'               33   0.9   0.8
        7   'PLAID   CYMRU'      7   0.2   0.2
        8   'ECOLOGY PARTY'      2   0.1   0.1
        9   'NATNL   FRONT'      2   0.1   0.1
       10   'COMNIST PARTY'      0   0.0   0.0
       95 M 'REFUSED'           92         2.3
       97 M 'OTHER   PARTY'      2         0.1
    
    ================================================================================
    
       lrself 'LEFT,RIGHT SCALE: R'S POSITION 1983'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: interval
       Missing values: 95, 96, 97, 98, 99
    
       Values and labels            N Valid Total
                                                 
       -10   'LEFT'                69   2.0   1.7
         0   'MID POINT'         2130  61.3  53.9
        10   'RIGHT'               74   2.1   1.9
        95 M 'NT ASKD RSN GIVN'    35         0.9
        96 M 'REFUSED'             19         0.5
        97 M 'TO RIGHT OF 10'       1         0.0
        98 M 'DK'                 393         9.9
        99 M 'NA'                  35         0.9
                                                 
            Min: -10.000                         
            Max:  10.000                         
           Mean:   0.154                         
       Std.Dev.:   3.174                         
    
    ================================================================================
    
       fglclass 'FATHER'S GOLDTHORPE-LLEWELLYN CLASS'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: 0
    
       Values and labels              N Valid Total
                                                   
       0 M 'NOT CLASSIFIED'         494        12.5
       1   'HGH GRD PROF & MAN'     337   9.7   8.5
       2   'LOW GRADE PROF & MAN'   275   7.9   7.0
       3   'ROUTINE NONMANUAL'      237   6.8   6.0
       4   'PETTY BOURGEOISIE'      483  14.0  12.2
       5   'MANUAL FOREMEN ETC'     333   9.6   8.4
       6   'SKILLED MANUAL'         961  27.8  24.3
       7   'SEMI & UNSKIL MANUAL'   835  24.1  21.1
    
    ================================================================================
    
       rglclass 'RESPONDENT'S GOLDTHORPE-LLEWELLYN CLASS'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: 0
    
       Values and labels              N Valid Total
                                                   
       0 M 'NOT CLASSIFIED'         203         5.1
       1   'HGH GRD PROF & MAN'     310   8.3   7.8
       2   'LOW GRADE PROF & MAN'   614  16.4  15.5
       3   'ROUTINE NONMANUAL'     1137  30.3  28.7
       4   'PETTY BOURGEOISIE'      244   6.5   6.2
       5   'MANUAL FOREMEN ETC'     227   6.1   5.7
       6   'SKILLED MANUAL'         413  11.0  10.4
       7   'SEMI & UNSKIL MANUAL'   807  21.5  20.4
    
    ================================================================================
    
       sglclass 'SPOUSE'S GOLDTHORPE-LLEWELLYN CLASS'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: 0
    
       Values and labels              N Valid Total
                                                   
       0 M 'NOT CLASSIFIED'        1350        34.1
       1   'HGH GRD PROF & MAN'     233   8.9   5.9
       2   'LOW GRADE PROF & MAN'   426  16.4  10.8
       3   'ROUTINE NONMANUAL'      732  28.1  18.5
       4   'PETTY BOURGEOISIE'      199   7.6   5.0
       5   'MANUAL FOREMEN ETC'     145   5.6   3.7
       6   'SKILLED MANUAL'         348  13.4   8.8
       7   'SEMI & UNSKIL MANUAL'   522  20.0  13.2
    
    ================================================================================
    
       ethnicity 'RESPONDENT'S ETHNIC ORIGIN'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: 9
    
       Values and labels          N Valid Total
                                               
       1   'WHITE   EUROPEAN'  3855  97.6  97.5
       2   'INDIAN  ETC'         51   1.3   1.3
       3   'BLACK   ETC'         33   0.8   0.8
       4   'OTHER   INC CHIN'    10   0.3   0.3
       9 M 'NA'                   6         0.2
    
    ================================================================================
    
       gender 'RESPONDENT'S SEX'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: 9
    
       Values and labels       N Percent
                                        
       1   'MALE'           1871    47.3
       2   'FEMALE'         2084    52.7
    
    ================================================================================
    
       age 'RESPONDENT'S AGE LAST BIRTHDAY'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: interval
       Missing values: 98, 99
    
       Values and labels       N Percent
                                        
       99 M 'NA'              20     0.5
                                        
            Min: 18.000                 
            Max: 93.000                 
           Mean: 45.433                 
       Std.Dev.: 17.817                 
    
    ================================================================================
    
       educ 'AGE RESPONDENT LEFT SCHOOL'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: interval
    
       Values and labels          N Percent
                                           
        0 'NO SCHOOLING'          3     0.1
       98 'STILL AT SCHOOL'       2     0.1
       99 'NA'                    7     0.2
                                           
            Min:  0.000                    
            Max: 99.000                    
           Mean: 15.636                    
       Std.Dev.:  4.238                    
    
    ================================================================================
    
       religion 'RESPONDENT'S RELIGION'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: 99
    
       Values and labels           N Valid Total
                                                
        0   'NO RELIG'          1009  25.6  25.5
        1   'XIAN NO DENOM'       66   1.7   1.7
        2   'ROMAN   CATHOLIC'   453  11.5  11.5
        3   'C OF E,W ETC'      1767  44.8  44.7
        4   'CHURCH  SCOTLAND'   223   5.7   5.6
        5   'METHODST'           185   4.7   4.7
        6   'BAPTIST'             61   1.5   1.5
        7   'URC ETC'             41   1.0   1.0
        8   'OTHER   XIAN'        71   1.8   1.8
        9   'J WITNESS'           11   0.3   0.3
       12   'JEW'                 11   0.3   0.3
       13   'HINDU'               20   0.5   0.5
       14   'ISLAM   MOSLEM'      16   0.4   0.4
       15   'SIKH'                 8   0.2   0.2
       16   'BUDDHIST'             0   0.0   0.0
       97   'OTHER   NON XIAN'     2   0.1   0.1
       99 M 'NA'                  11         0.3
    
    ================================================================================
    
       religatt '[IF RELIGION] FREQUENCY OF ATTENDANCE'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: 7, 8, 9
    
       Values and labels         N Valid Total
                                              
       0   'NEVER'            1089  38.2  27.5
       1   'ONCE+ A WEEK'      475  16.7  12.0
       2   'SEV TIME A MTH'    114   4.0   2.9
       3   'ONCE+ A MTH'       176   6.2   4.5
       4   'SEV TIME YR'       424  14.9  10.7
       5   'ONCE+ A YR'        283   9.9   7.2
       6   'LESS TH ONCE YR'   290  10.2   7.3
       7 M 'VARIES  DK'         74         1.9
       8 M 'SKIPPED'          1020        25.8
       9 M 'NA'                 10         0.3
    
    In [13]:
    options(jupyter.rich_display=TRUE)
    show_html(codebook(BES.1983.classvot)) # Same information in HTML format
    

    voted'DID RESPONDENT VOTE 1983'

    Storage mode: double
    Measurement: nominal
    Missing values: 9

    Values and labels N Percent
    1 'YES' 3295 83 . 3
    2 'NO' 660 16 . 7

    vote'[IF VOTED] PARTY VOTED FOR'

    Storage mode: double
    Measurement: nominal
    Missing values: 95, 96, 97, 98, 99

    Values and labels N Valid Total
    0 'SKIPPED' 660 17 . 1 16 . 7
    1 'CON' 1432 37 . 1 36 . 2
    2 'LAB' 937 24 . 3 23 . 7
    3 'ALLIANCE' 416 10 . 8 10 . 5
    4 'LIB' 278 7 . 2 7 . 0
    5 'SOCIAL DEMOCR' 94 2 . 4 2 . 4
    6 'SNP' 33 0 . 9 0 . 8
    7 'PLAID CYMRU' 7 0 . 2 0 . 2
    8 'ECOLOGY PARTY' 2 0 . 1 0 . 1
    9 'NATNL FRONT' 2 0 . 1 0 . 1
    10 'COMNIST PARTY' 0 0 . 0 0 . 0
    95 M 'REFUSED' 92 2 . 3
    97 M 'OTHER PARTY' 2 0 . 1

    lrself'LEFT,RIGHT SCALE: R'S POSITION 1983'

    Storage mode: double
    Measurement: interval
    Missing values: 95, 96, 97, 98, 99

    Values and labels N Valid Total
    -10 'LEFT' 69 2 . 0 1 . 7
    0 'MID POINT' 2130 61 . 3 53 . 9
    10 'RIGHT' 74 2 . 1 1 . 9
    95 M 'NT ASKD RSN GIVN' 35 0 . 9
    96 M 'REFUSED' 19 0 . 5
    97 M 'TO RIGHT OF 10' 1 0 . 0
    98 M 'DK' 393 9 . 9
    99 M 'NA' 35 0 . 9

    Min: -10 . 000
    Max: 10 . 000
    Mean: 0 . 154
    Std.Dev.: 3 . 174

    fglclass'FATHER'S GOLDTHORPE-LLEWELLYN CLASS'

    Storage mode: double
    Measurement: nominal
    Missing values: 0

    Values and labels N Valid Total
    0 M 'NOT CLASSIFIED' 494 12 . 5
    1 'HGH GRD PROF & MAN' 337 9 . 7 8 . 5
    2 'LOW GRADE PROF & MAN' 275 7 . 9 7 . 0
    3 'ROUTINE NONMANUAL' 237 6 . 8 6 . 0
    4 'PETTY BOURGEOISIE' 483 14 . 0 12 . 2
    5 'MANUAL FOREMEN ETC' 333 9 . 6 8 . 4
    6 'SKILLED MANUAL' 961 27 . 8 24 . 3
    7 'SEMI & UNSKIL MANUAL' 835 24 . 1 21 . 1

    rglclass'RESPONDENT'S GOLDTHORPE-LLEWELLYN CLASS'

    Storage mode: double
    Measurement: nominal
    Missing values: 0

    Values and labels N Valid Total
    0 M 'NOT CLASSIFIED' 203 5 . 1
    1 'HGH GRD PROF & MAN' 310 8 . 3 7 . 8
    2 'LOW GRADE PROF & MAN' 614 16 . 4 15 . 5
    3 'ROUTINE NONMANUAL' 1137 30 . 3 28 . 7
    4 'PETTY BOURGEOISIE' 244 6 . 5 6 . 2
    5 'MANUAL FOREMEN ETC' 227 6 . 1 5 . 7
    6 'SKILLED MANUAL' 413 11 . 0 10 . 4
    7 'SEMI & UNSKIL MANUAL' 807 21 . 5 20 . 4

    sglclass'SPOUSE'S GOLDTHORPE-LLEWELLYN CLASS'

    Storage mode: double
    Measurement: nominal
    Missing values: 0

    Values and labels N Valid Total
    0 M 'NOT CLASSIFIED' 1350 34 . 1
    1 'HGH GRD PROF & MAN' 233 8 . 9 5 . 9
    2 'LOW GRADE PROF & MAN' 426 16 . 4 10 . 8
    3 'ROUTINE NONMANUAL' 732 28 . 1 18 . 5
    4 'PETTY BOURGEOISIE' 199 7 . 6 5 . 0
    5 'MANUAL FOREMEN ETC' 145 5 . 6 3 . 7
    6 'SKILLED MANUAL' 348 13 . 4 8 . 8
    7 'SEMI & UNSKIL MANUAL' 522 20 . 0 13 . 2

    ethnicity'RESPONDENT'S ETHNIC ORIGIN'

    Storage mode: double
    Measurement: nominal
    Missing values: 9

    Values and labels N Valid Total
    1 'WHITE EUROPEAN' 3855 97 . 6 97 . 5
    2 'INDIAN ETC' 51 1 . 3 1 . 3
    3 'BLACK ETC' 33 0 . 8 0 . 8
    4 'OTHER INC CHIN' 10 0 . 3 0 . 3
    9 M 'NA' 6 0 . 2

    gender'RESPONDENT'S SEX'

    Storage mode: double
    Measurement: nominal
    Missing values: 9

    Values and labels N Percent
    1 'MALE' 1871 47 . 3
    2 'FEMALE' 2084 52 . 7

    age'RESPONDENT'S AGE LAST BIRTHDAY'

    Storage mode: double
    Measurement: interval
    Missing values: 98, 99

    Values and labels N Percent
    99 M 'NA' 20 0 . 5

    Min: 18 . 000
    Max: 93 . 000
    Mean: 45 . 433
    Std.Dev.: 17 . 817

    educ'AGE RESPONDENT LEFT SCHOOL'

    Storage mode: double
    Measurement: interval

    Values and labels N Percent
    0 'NO SCHOOLING' 3 0 . 1
    98 'STILL AT SCHOOL' 2 0 . 1
    99 'NA' 7 0 . 2

    Min: 0 . 000
    Max: 99 . 000
    Mean: 15 . 636
    Std.Dev.: 4 . 238

    religion'RESPONDENT'S RELIGION'

    Storage mode: double
    Measurement: nominal
    Missing values: 99

    Values and labels N Valid Total
    0 'NO RELIG' 1009 25 . 6 25 . 5
    1 'XIAN NO DENOM' 66 1 . 7 1 . 7
    2 'ROMAN CATHOLIC' 453 11 . 5 11 . 5
    3 'C OF E,W ETC' 1767 44 . 8 44 . 7
    4 'CHURCH SCOTLAND' 223 5 . 7 5 . 6
    5 'METHODST' 185 4 . 7 4 . 7
    6 'BAPTIST' 61 1 . 5 1 . 5
    7 'URC ETC' 41 1 . 0 1 . 0
    8 'OTHER XIAN' 71 1 . 8 1 . 8
    9 'J WITNESS' 11 0 . 3 0 . 3
    12 'JEW' 11 0 . 3 0 . 3
    13 'HINDU' 20 0 . 5 0 . 5
    14 'ISLAM MOSLEM' 16 0 . 4 0 . 4
    15 'SIKH' 8 0 . 2 0 . 2
    16 'BUDDHIST' 0 0 . 0 0 . 0
    97 'OTHER NON XIAN' 2 0 . 1 0 . 1
    99 M 'NA' 11 0 . 3

    religatt'[IF RELIGION] FREQUENCY OF ATTENDANCE'

    Storage mode: double
    Measurement: nominal
    Missing values: 7, 8, 9

    Values and labels N Valid Total
    0 'NEVER' 1089 38 . 2 27 . 5
    1 'ONCE+ A WEEK' 475 16 . 7 12 . 0
    2 'SEV TIME A MTH' 114 4 . 0 2 . 9
    3 'ONCE+ A MTH' 176 6 . 2 4 . 5
    4 'SEV TIME YR' 424 14 . 9 10 . 7
    5 'ONCE+ A YR' 283 9 . 9 7 . 2
    6 'LESS TH ONCE YR' 290 10 . 2 7 . 3
    7 M 'VARIES DK' 74 1 . 9
    8 M 'SKIPPED' 1020 25 . 8
    9 M 'NA' 10 0 . 3

    In [14]:
    save(BES.1983.classvot,
         file="BES-1983-classvot.RData")
    
    In [ ]:
    
    

Importing data from the American National Election Study in ASCII format with acompanying SPSS code

  • Script file: importing-BES-data.R

    Required data files: anes2008TS_dat.txt, anes2008TS_col.sps, anes2008TS_lab.sps, anes2008TS_cod.sps, anes2008TS_md.sps which are available from https://electionstudies.org/data-center/2008-time-series-study/ (as ASCII variant of the data). You will need to register first in order to be able to download the data.

    The script makes use of the memisc package, which is available from https://cran.r-project.org/package=memisc

  • Interactive notebook:

    In [1]:
    options(jupyter.rich_display=FALSE) # Create output as usual in R
    

    In order to run this notebook successfully, you have to download the 1983 British Election Study from the BES website and upload it to the virtual machine on which this notebook runs. To do this,

    1. pull down the "File" menu item and select "Open"
    2. An overview of the folder that contains the notebook opens.
    3. The folder view has a button labelled "Upload". Use this to upload the file that you downloaded from the BES website. Its name should be 83BES.sav.

    Note that the uploaded data will disappear, once you "Quit" the notebook (and the Jupyter instance).

    In [2]:
    library(memisc) # The functions used here are in this package.
    
    Loading required package: lattice
    
    Loading required package: MASS
    
    
    Attaching package: ‘memisc’
    
    
    The following objects are masked from ‘package:stats’:
    
        contr.sum, contr.treatment, contrasts
    
    
    The following object is masked from ‘package:base’:
    
        as.array
    
    
    
    In [3]:
    # The first step: Optainin the location and description of the survey data file
    BES.1983.por <- spss.portable.file("83BES.sav")
    
    In [4]:
    description(BES.1983.por[1:30])
    
     ID1      'SERIAL NUMBER'                           
     RECORD1  'CARD NUMBER'                             
     AREANO   'AREA NUMBER'                             
     PANO     'PA CONSTITUENCY NUMBER'                  
     MOVER    'MOVER-REISSUE CODE'                      
     INT      'INTERVIEWER NUMBER'                      
     FGLCLASS 'FATHER'S GOLDTHORPE-LLEWELLYN CLASS'     
     RGLCLASS 'RESPONDENT'S GOLDTHORPE-LLEWELLYN CLASS' 
     SGLCLASS 'SPOUSE'S GOLDTHORPE-LLEWELLYN CLASS'     
     FGHCLASS 'FATHER'S GOLDTHORPE-HEATH CLASS'         
     RGHCLASS 'RESPONDENT'S GOLDTHORPE-HEATH CLASS'     
     SGHCLASS 'SPOUSE'S GOLDTHORPE-HEATH CLASS'         
     Q1       'CARE WHICH PARTY WON'                    
     Q2A      'FOLLOWED ELECTION BROADCASTS'            
     Q2B      'READ ABOUT CAMPAIGN IN NEWSPAPER'        
     Q2C      '[IF READ NEWSPP] WHICH DAILY ON CAMPAIGN'
     Q2D      'HEARD CANDIDATE AT POLITICAL MEETING'    
     Q2E      'CANVASSED FOR CANDIDATE'                 
     Q2F      'PUT UP POLITICAL PARTY POSTER'           
     Q2G      'CANVASSER CALLED DURING CAMPAIGN'        
     Q2H1     'CON CANVASSER CALLED'                    
     Q2H2     'LAB CANVASSER CALLED'                    
     Q2H3     'ALLIANCE CANVASSER CALLED'               
     Q2H4     'LIB CANVASSER CALLED'                    
     Q2H5     'SOCIAL DEMOCRACTIC CANVASSER CALLED'     
     Q2H6     'SNP CANVASSER CALLED'                    
     Q2H7     'PLAID CYMRU CANVASSER CALLED'            
     Q2H8     'OTHER PARTY CANVASSER CALLED'            
     Q2H9     'ECOLOGY PARTY CANVASSER CALLED'          
     Q2H10    'NATIONAL FRONT CANVASSER CALLED'         
    
    In [5]:
    # The second step: Loading a subset of the data - only the variables that are
    # really needed for the analysis.
    BES.1983.classvot <- subset(
        BES.1983.por,
        select=c(
            voted          = Q7A,
            vote           = Q9A,
            lrself         = Q46G,
            fglclass       = FGLCLASS,
            rglclass       = RGLCLASS,
            sglclass       = SGLCLASS,
            ethnicity      = Q64A,
            gender         = Q64B,
            age            = Q56,
            educ           = Q59A,
            religion       = Q63A,
            religatt       = Q63B
        ))
    description(BES.1983.classvot)
    
     voted     'DID RESPONDENT VOTE 1983'               
     vote      '[IF VOTED] PARTY VOTED FOR'             
     lrself    'LEFT,RIGHT SCALE: R'S POSITION 1983'    
     fglclass  'FATHER'S GOLDTHORPE-LLEWELLYN CLASS'    
     rglclass  'RESPONDENT'S GOLDTHORPE-LLEWELLYN CLASS'
     sglclass  'SPOUSE'S GOLDTHORPE-LLEWELLYN CLASS'    
     ethnicity 'RESPONDENT'S ETHNIC ORIGIN'             
     gender    'RESPONDENT'S SEX'                       
     age       'RESPONDENT'S AGE LAST BIRTHDAY'         
     educ      'AGE RESPONDENT LEFT SCHOOL'             
     religion  'RESPONDENT'S RELIGION'                  
     religatt  '[IF RELIGION] FREQUENCY OF ATTENDANCE'  
    
    In [6]:
    # Declaring the measurement level of a single variable
    measurement(BES.1983.classvot$lrself) <- "interval"
    
    In [7]:
    # Declaring the measurement level of several variables
    BES.1983.classvot <- within(BES.1983.classvot,{
       measurement(lrself) <- "interval"
       measurement(age) <- "interval"
       measurement(educ) <- "interval"
    })
    
    In [8]:
    # Declaring the measurement level of several variables using a loop
    BES.1983.classvot <- within(BES.1983.classvot,{
       foreach(var=c(lrself,age,educ),{
               measurement(var) <- "interval"
       })
    })
    
    In [9]:
    # Obtaining the codebook a single variable
    codebook(BES.1983.classvot["age"])
    
    ================================================================================
    
       age 'RESPONDENT'S AGE LAST BIRTHDAY'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: interval
    
       Values and labels       N Percent
                                        
       99 'NA'                20     0.5
                                        
            Min: 18.000                 
            Max: 99.000                 
           Mean: 45.704                 
       Std.Dev.: 18.174                 
    
    In [10]:
    # Declaring the missing values for this variable
    missing.values(BES.1983.classvot$age) <- 99
    
    In [11]:
    # Declaring several missing values
    BES.1983.classvot <- within(BES.1983.classvot,{
        missing.values(voted)     <- 9
        missing.values(vote)      <- 95:99
        missing.values(lrself)    <- 95:99
        missing.values(fglclass)  <- 0
        missing.values(rglclass)  <- 0
        missing.values(sglclass)  <- 0
        missing.values(ethnicity) <- 9
        missing.values(gender)    <- 9
        missing.values(age)       <- 9
        missing.values(age)       <- 98:99
        missing.values(religion)  <- 99
        missing.values(religatt)  <- 7:9
    })
    
    In [12]:
    # The codebook of the resulting data set object
    codebook(BES.1983.classvot)
    
    ================================================================================
    
       voted 'DID RESPONDENT VOTE 1983'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: 9
    
       Values and labels       N Percent
                                        
       1   'YES'            3295    83.3
       2   'NO'              660    16.7
    
    ================================================================================
    
       vote '[IF VOTED] PARTY VOTED FOR'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: 95, 96, 97, 98, 99
    
       Values and labels         N Valid Total
                                              
        0   'SKIPPED'          660  17.1  16.7
        1   'CON'             1432  37.1  36.2
        2   'LAB'              937  24.3  23.7
        3   'ALLIANCE'         416  10.8  10.5
        4   'LIB'              278   7.2   7.0
        5   'SOCIAL  DEMOCR'    94   2.4   2.4
        6   'SNP'               33   0.9   0.8
        7   'PLAID   CYMRU'      7   0.2   0.2
        8   'ECOLOGY PARTY'      2   0.1   0.1
        9   'NATNL   FRONT'      2   0.1   0.1
       10   'COMNIST PARTY'      0   0.0   0.0
       95 M 'REFUSED'           92         2.3
       97 M 'OTHER   PARTY'      2         0.1
    
    ================================================================================
    
       lrself 'LEFT,RIGHT SCALE: R'S POSITION 1983'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: interval
       Missing values: 95, 96, 97, 98, 99
    
       Values and labels            N Valid Total
                                                 
       -10   'LEFT'                69   2.0   1.7
         0   'MID POINT'         2130  61.3  53.9
        10   'RIGHT'               74   2.1   1.9
        95 M 'NT ASKD RSN GIVN'    35         0.9
        96 M 'REFUSED'             19         0.5
        97 M 'TO RIGHT OF 10'       1         0.0
        98 M 'DK'                 393         9.9
        99 M 'NA'                  35         0.9
                                                 
            Min: -10.000                         
            Max:  10.000                         
           Mean:   0.154                         
       Std.Dev.:   3.174                         
    
    ================================================================================
    
       fglclass 'FATHER'S GOLDTHORPE-LLEWELLYN CLASS'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: 0
    
       Values and labels              N Valid Total
                                                   
       0 M 'NOT CLASSIFIED'         494        12.5
       1   'HGH GRD PROF & MAN'     337   9.7   8.5
       2   'LOW GRADE PROF & MAN'   275   7.9   7.0
       3   'ROUTINE NONMANUAL'      237   6.8   6.0
       4   'PETTY BOURGEOISIE'      483  14.0  12.2
       5   'MANUAL FOREMEN ETC'     333   9.6   8.4
       6   'SKILLED MANUAL'         961  27.8  24.3
       7   'SEMI & UNSKIL MANUAL'   835  24.1  21.1
    
    ================================================================================
    
       rglclass 'RESPONDENT'S GOLDTHORPE-LLEWELLYN CLASS'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: 0
    
       Values and labels              N Valid Total
                                                   
       0 M 'NOT CLASSIFIED'         203         5.1
       1   'HGH GRD PROF & MAN'     310   8.3   7.8
       2   'LOW GRADE PROF & MAN'   614  16.4  15.5
       3   'ROUTINE NONMANUAL'     1137  30.3  28.7
       4   'PETTY BOURGEOISIE'      244   6.5   6.2
       5   'MANUAL FOREMEN ETC'     227   6.1   5.7
       6   'SKILLED MANUAL'         413  11.0  10.4
       7   'SEMI & UNSKIL MANUAL'   807  21.5  20.4
    
    ================================================================================
    
       sglclass 'SPOUSE'S GOLDTHORPE-LLEWELLYN CLASS'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: 0
    
       Values and labels              N Valid Total
                                                   
       0 M 'NOT CLASSIFIED'        1350        34.1
       1   'HGH GRD PROF & MAN'     233   8.9   5.9
       2   'LOW GRADE PROF & MAN'   426  16.4  10.8
       3   'ROUTINE NONMANUAL'      732  28.1  18.5
       4   'PETTY BOURGEOISIE'      199   7.6   5.0
       5   'MANUAL FOREMEN ETC'     145   5.6   3.7
       6   'SKILLED MANUAL'         348  13.4   8.8
       7   'SEMI & UNSKIL MANUAL'   522  20.0  13.2
    
    ================================================================================
    
       ethnicity 'RESPONDENT'S ETHNIC ORIGIN'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: 9
    
       Values and labels          N Valid Total
                                               
       1   'WHITE   EUROPEAN'  3855  97.6  97.5
       2   'INDIAN  ETC'         51   1.3   1.3
       3   'BLACK   ETC'         33   0.8   0.8
       4   'OTHER   INC CHIN'    10   0.3   0.3
       9 M 'NA'                   6         0.2
    
    ================================================================================
    
       gender 'RESPONDENT'S SEX'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: 9
    
       Values and labels       N Percent
                                        
       1   'MALE'           1871    47.3
       2   'FEMALE'         2084    52.7
    
    ================================================================================
    
       age 'RESPONDENT'S AGE LAST BIRTHDAY'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: interval
       Missing values: 98, 99
    
       Values and labels       N Percent
                                        
       99 M 'NA'              20     0.5
                                        
            Min: 18.000                 
            Max: 93.000                 
           Mean: 45.433                 
       Std.Dev.: 17.817                 
    
    ================================================================================
    
       educ 'AGE RESPONDENT LEFT SCHOOL'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: interval
    
       Values and labels          N Percent
                                           
        0 'NO SCHOOLING'          3     0.1
       98 'STILL AT SCHOOL'       2     0.1
       99 'NA'                    7     0.2
                                           
            Min:  0.000                    
            Max: 99.000                    
           Mean: 15.636                    
       Std.Dev.:  4.238                    
    
    ================================================================================
    
       religion 'RESPONDENT'S RELIGION'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: 99
    
       Values and labels           N Valid Total
                                                
        0   'NO RELIG'          1009  25.6  25.5
        1   'XIAN NO DENOM'       66   1.7   1.7
        2   'ROMAN   CATHOLIC'   453  11.5  11.5
        3   'C OF E,W ETC'      1767  44.8  44.7
        4   'CHURCH  SCOTLAND'   223   5.7   5.6
        5   'METHODST'           185   4.7   4.7
        6   'BAPTIST'             61   1.5   1.5
        7   'URC ETC'             41   1.0   1.0
        8   'OTHER   XIAN'        71   1.8   1.8
        9   'J WITNESS'           11   0.3   0.3
       12   'JEW'                 11   0.3   0.3
       13   'HINDU'               20   0.5   0.5
       14   'ISLAM   MOSLEM'      16   0.4   0.4
       15   'SIKH'                 8   0.2   0.2
       16   'BUDDHIST'             0   0.0   0.0
       97   'OTHER   NON XIAN'     2   0.1   0.1
       99 M 'NA'                  11         0.3
    
    ================================================================================
    
       religatt '[IF RELIGION] FREQUENCY OF ATTENDANCE'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: 7, 8, 9
    
       Values and labels         N Valid Total
                                              
       0   'NEVER'            1089  38.2  27.5
       1   'ONCE+ A WEEK'      475  16.7  12.0
       2   'SEV TIME A MTH'    114   4.0   2.9
       3   'ONCE+ A MTH'       176   6.2   4.5
       4   'SEV TIME YR'       424  14.9  10.7
       5   'ONCE+ A YR'        283   9.9   7.2
       6   'LESS TH ONCE YR'   290  10.2   7.3
       7 M 'VARIES  DK'         74         1.9
       8 M 'SKIPPED'          1020        25.8
       9 M 'NA'                 10         0.3
    
    In [13]:
    options(jupyter.rich_display=TRUE)
    show_html(codebook(BES.1983.classvot)) # Same information in HTML format
    

    voted'DID RESPONDENT VOTE 1983'

    Storage mode: double
    Measurement: nominal
    Missing values: 9

    Values and labels N Percent
    1 'YES' 3295 83 . 3
    2 'NO' 660 16 . 7

    vote'[IF VOTED] PARTY VOTED FOR'

    Storage mode: double
    Measurement: nominal
    Missing values: 95, 96, 97, 98, 99

    Values and labels N Valid Total
    0 'SKIPPED' 660 17 . 1 16 . 7
    1 'CON' 1432 37 . 1 36 . 2
    2 'LAB' 937 24 . 3 23 . 7
    3 'ALLIANCE' 416 10 . 8 10 . 5
    4 'LIB' 278 7 . 2 7 . 0
    5 'SOCIAL DEMOCR' 94 2 . 4 2 . 4
    6 'SNP' 33 0 . 9 0 . 8
    7 'PLAID CYMRU' 7 0 . 2 0 . 2
    8 'ECOLOGY PARTY' 2 0 . 1 0 . 1
    9 'NATNL FRONT' 2 0 . 1 0 . 1
    10 'COMNIST PARTY' 0 0 . 0 0 . 0
    95 M 'REFUSED' 92 2 . 3
    97 M 'OTHER PARTY' 2 0 . 1

    lrself'LEFT,RIGHT SCALE: R'S POSITION 1983'

    Storage mode: double
    Measurement: interval
    Missing values: 95, 96, 97, 98, 99

    Values and labels N Valid Total
    -10 'LEFT' 69 2 . 0 1 . 7
    0 'MID POINT' 2130 61 . 3 53 . 9
    10 'RIGHT' 74 2 . 1 1 . 9
    95 M 'NT ASKD RSN GIVN' 35 0 . 9
    96 M 'REFUSED' 19 0 . 5
    97 M 'TO RIGHT OF 10' 1 0 . 0
    98 M 'DK' 393 9 . 9
    99 M 'NA' 35 0 . 9

    Min: -10 . 000
    Max: 10 . 000
    Mean: 0 . 154
    Std.Dev.: 3 . 174

    fglclass'FATHER'S GOLDTHORPE-LLEWELLYN CLASS'

    Storage mode: double
    Measurement: nominal
    Missing values: 0

    Values and labels N Valid Total
    0 M 'NOT CLASSIFIED' 494 12 . 5
    1 'HGH GRD PROF & MAN' 337 9 . 7 8 . 5
    2 'LOW GRADE PROF & MAN' 275 7 . 9 7 . 0
    3 'ROUTINE NONMANUAL' 237 6 . 8 6 . 0
    4 'PETTY BOURGEOISIE' 483 14 . 0 12 . 2
    5 'MANUAL FOREMEN ETC' 333 9 . 6 8 . 4
    6 'SKILLED MANUAL' 961 27 . 8 24 . 3
    7 'SEMI & UNSKIL MANUAL' 835 24 . 1 21 . 1

    rglclass'RESPONDENT'S GOLDTHORPE-LLEWELLYN CLASS'

    Storage mode: double
    Measurement: nominal
    Missing values: 0

    Values and labels N Valid Total
    0 M 'NOT CLASSIFIED' 203 5 . 1
    1 'HGH GRD PROF & MAN' 310 8 . 3 7 . 8
    2 'LOW GRADE PROF & MAN' 614 16 . 4 15 . 5
    3 'ROUTINE NONMANUAL' 1137 30 . 3 28 . 7
    4 'PETTY BOURGEOISIE' 244 6 . 5 6 . 2
    5 'MANUAL FOREMEN ETC' 227 6 . 1 5 . 7
    6 'SKILLED MANUAL' 413 11 . 0 10 . 4
    7 'SEMI & UNSKIL MANUAL' 807 21 . 5 20 . 4

    sglclass'SPOUSE'S GOLDTHORPE-LLEWELLYN CLASS'

    Storage mode: double
    Measurement: nominal
    Missing values: 0

    Values and labels N Valid Total
    0 M 'NOT CLASSIFIED' 1350 34 . 1
    1 'HGH GRD PROF & MAN' 233 8 . 9 5 . 9
    2 'LOW GRADE PROF & MAN' 426 16 . 4 10 . 8
    3 'ROUTINE NONMANUAL' 732 28 . 1 18 . 5
    4 'PETTY BOURGEOISIE' 199 7 . 6 5 . 0
    5 'MANUAL FOREMEN ETC' 145 5 . 6 3 . 7
    6 'SKILLED MANUAL' 348 13 . 4 8 . 8
    7 'SEMI & UNSKIL MANUAL' 522 20 . 0 13 . 2

    ethnicity'RESPONDENT'S ETHNIC ORIGIN'

    Storage mode: double
    Measurement: nominal
    Missing values: 9

    Values and labels N Valid Total
    1 'WHITE EUROPEAN' 3855 97 . 6 97 . 5
    2 'INDIAN ETC' 51 1 . 3 1 . 3
    3 'BLACK ETC' 33 0 . 8 0 . 8
    4 'OTHER INC CHIN' 10 0 . 3 0 . 3
    9 M 'NA' 6 0 . 2

    gender'RESPONDENT'S SEX'

    Storage mode: double
    Measurement: nominal
    Missing values: 9

    Values and labels N Percent
    1 'MALE' 1871 47 . 3
    2 'FEMALE' 2084 52 . 7

    age'RESPONDENT'S AGE LAST BIRTHDAY'

    Storage mode: double
    Measurement: interval
    Missing values: 98, 99

    Values and labels N Percent
    99 M 'NA' 20 0 . 5

    Min: 18 . 000
    Max: 93 . 000
    Mean: 45 . 433
    Std.Dev.: 17 . 817

    educ'AGE RESPONDENT LEFT SCHOOL'

    Storage mode: double
    Measurement: interval

    Values and labels N Percent
    0 'NO SCHOOLING' 3 0 . 1
    98 'STILL AT SCHOOL' 2 0 . 1
    99 'NA' 7 0 . 2

    Min: 0 . 000
    Max: 99 . 000
    Mean: 15 . 636
    Std.Dev.: 4 . 238

    religion'RESPONDENT'S RELIGION'

    Storage mode: double
    Measurement: nominal
    Missing values: 99

    Values and labels N Valid Total
    0 'NO RELIG' 1009 25 . 6 25 . 5
    1 'XIAN NO DENOM' 66 1 . 7 1 . 7
    2 'ROMAN CATHOLIC' 453 11 . 5 11 . 5
    3 'C OF E,W ETC' 1767 44 . 8 44 . 7
    4 'CHURCH SCOTLAND' 223 5 . 7 5 . 6
    5 'METHODST' 185 4 . 7 4 . 7
    6 'BAPTIST' 61 1 . 5 1 . 5
    7 'URC ETC' 41 1 . 0 1 . 0
    8 'OTHER XIAN' 71 1 . 8 1 . 8
    9 'J WITNESS' 11 0 . 3 0 . 3
    12 'JEW' 11 0 . 3 0 . 3
    13 'HINDU' 20 0 . 5 0 . 5
    14 'ISLAM MOSLEM' 16 0 . 4 0 . 4
    15 'SIKH' 8 0 . 2 0 . 2
    16 'BUDDHIST' 0 0 . 0 0 . 0
    97 'OTHER NON XIAN' 2 0 . 1 0 . 1
    99 M 'NA' 11 0 . 3

    religatt'[IF RELIGION] FREQUENCY OF ATTENDANCE'

    Storage mode: double
    Measurement: nominal
    Missing values: 7, 8, 9

    Values and labels N Valid Total
    0 'NEVER' 1089 38 . 2 27 . 5
    1 'ONCE+ A WEEK' 475 16 . 7 12 . 0
    2 'SEV TIME A MTH' 114 4 . 0 2 . 9
    3 'ONCE+ A MTH' 176 6 . 2 4 . 5
    4 'SEV TIME YR' 424 14 . 9 10 . 7
    5 'ONCE+ A YR' 283 9 . 9 7 . 2
    6 'LESS TH ONCE YR' 290 10 . 2 7 . 3
    7 M 'VARIES DK' 74 1 . 9
    8 M 'SKIPPED' 1020 25 . 8
    9 M 'NA' 10 0 . 3

    In [14]:
    save(BES.1983.classvot,
         file="BES-1983-classvot.RData")
    
    In [ ]:
    
    

Recoding and other transformations

Recoding data from the British Election Study

  • Script file: recoding-BES.R

    Required data file: BES-1983-classvot.RData, which is created in the first example.

    The script makes use of the memisc package, which is available from https://cran.r-project.org/package=memisc

  • Interactive notebook:

    In [1]:
    options(jupyter.rich_display=FALSE) # Create output as usual in R
    library(memisc)
    
    Loading required package: lattice
    
    Loading required package: MASS
    
    
    Attaching package: ‘memisc’
    
    
    The following objects are masked from ‘package:stats’:
    
        contr.sum, contr.treatment, contrasts
    
    
    The following object is masked from ‘package:base’:
    
        as.array
    
    
    

    The following code picks up with the British Election Study data of the previous script. We use the data file created earlier.

    In [2]:
    load("BES-1983-classvot.RData")
    
    In [3]:
    # This code collapses the categories of the vote variable into just four:
    BES.1983.classvot <- within(BES.1983.classvot,{
        vote.new <- vote
        vote.new[vote %in% 3:5]        <- 3
        vote.new[vote %in% c(6:10,97)] <- 4
    })
    # Checking the result:
    codebook(BES.1983.classvot$vote.new)
    
    ================================================================================
    
       BES.1983.classvot$vote.new '[IF VOTED] PARTY VOTED FOR'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: 95, 96, 97, 98, 99
    
       Values and labels         N Valid Total
                                              
        0   'SKIPPED'          660  17.1  16.7
        1   'CON'             1432  37.1  36.2
        2   'LAB'              937  24.3  23.7
        3   'ALLIANCE'         788  20.4  19.9
        4   'LIB'               46   1.2   1.2
        5   'SOCIAL  DEMOCR'     0   0.0   0.0
        6   'SNP'                0   0.0   0.0
        7   'PLAID   CYMRU'      0   0.0   0.0
        8   'ECOLOGY PARTY'      0   0.0   0.0
        9   'NATNL   FRONT'      0   0.0   0.0
       10   'COMNIST PARTY'      0   0.0   0.0
       95 M 'REFUSED'           92         2.3
    
    In [4]:
    # It is somewhat more convenient to use the 'recode()' function from the
    # 'memisc' package:
    BES.1983.classvot <- within(BES.1983.classvot,{
        vote.new <- recode(vote,
                           3 <- 3:5,
                           4 <- c(6:10,97),
                           otherwise="copy"
                           )
    })
    # Checking the result:
    codebook(BES.1983.classvot$vote.new)
    
    ================================================================================
    
       BES.1983.classvot$vote.new '[IF VOTED] PARTY VOTED FOR'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: 95, 96, 97, 98, 99
    
       Values and labels     N Valid Total
                                          
        0   'SKIPPED'      660  17.1  16.7
        1   'CON'         1432  37.1  36.2
        2   'LAB'          937  24.3  23.7
        3   'ALLIANCE'     788  20.4  19.9
        4   'LIB'           46   1.2   1.2
       95 M 'REFUSED'       92         2.3
    
    In [5]:
    # Since 'BES.1983.classvot' is not a data frame, but a "data.set" object, we can
    # provide value labels while recoding:
    BES.1983.classvot <- within(BES.1983.classvot,{
        vote.new <- recode(vote,
                           Conservative  = 1 <- 1,
                           Labour        = 2 <- 2,
                           Alliance      = 3 <- 3:5,
                           Other         = 4 <- c(6:10,97),
                           "Didn't vote" = 5 <- 0,
                           DK            = 8 <- 98,
                           Refused       = 9 <- 95)
        missing.values(vote.new) <- c(5,9)
    })
    # Checking the result:
    codebook(BES.1983.classvot$vote.new)
    
    Warning message in recode(vote, Conservative = 1 <- 1, Labour = 2 <- 2, Alliance = 3 <- 3:5, :
    “recoding 8 <- 98 has no consequences”
    
    ================================================================================
    
       BES.1983.classvot$vote.new '[IF VOTED] PARTY VOTED FOR'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: 5, 9
    
       Values and labels      N Valid Total
                                           
       1   'Conservative'  1432  44.7  36.2
       2   'Labour'         937  29.3  23.7
       3   'Alliance'       788  24.6  19.9
       4   'Other'           46   1.4   1.2
       5 M 'Didn't vote'    660        16.7
       9 M 'Refused'         92         2.3
    
    In [ ]:
    
    

Combining variables using case distinctions

  • Script file: recoding-GLES.R

    Required data file: ZA6802_en_v3-0-1.sav, which is available from https://doi.org/10.4232/1.13236

    The script makes use of the memisc package, which is available from https://cran.r-project.org/package=memisc

  • Interactive notebook:

    In [1]:
    options(jupyter.rich_display=FALSE) # Create output as usual in R
    library(memisc)
    
    Loading required package: lattice
    
    Loading required package: MASS
    
    
    Attaching package: ‘memisc’
    
    
    The following objects are masked from ‘package:stats’:
    
        contr.sum, contr.treatment, contrasts
    
    
    The following object is masked from ‘package:base’:
    
        as.array
    
    
    

    The following code works with example data from the 2017 German Longitudinal Election study: It code combines pre- and post-election variables in the to a single party-preference variable for the first (candidate) vote and the second (list) vote.

    In order to run this notebook successfully, you have to download from GESIS and upload it to the virtual machine on which this notebook runs. To do this,

    1. pull down the "File" menu item and select "Open"
    2. An overview of the folder that contains the notebook opens.
    3. The folder view has a button labelled "Upload". Use this to upload the file that you downloaded from the BES website. Its name should be 83BES.sav.

    Note that the uploaded data will disappear, once you "Quit" the notebook (and the Jupyter instance).

    In [2]:
    gles2017.sav <- spss.system.file("ZA6802_en_v3-0-1.sav")
    description(gles2017.sav[1:30])
    
    File character set is 'UTF-8'.
    
    Converting character set to the local 'utf-8'.
    
    Warning message:
    “10 variables have duplicated labels:
      vn2d, v10, vn141_i88, vn141_i08, vn150_i88, vn150_i08, vn158_i88,
      vn158_i08, vn163_i88, vn163_i08”
    
     study        'Study number (ZA-No.)'                                               
     version      'GESIS Archive Version'                                               
     doi          'Digital Object Identifier'                                           
     year         'Survey year'                                                         
     field        'Field period'                                                        
     glescomp     'GLES component'                                                      
     survey       'Survey/wave'                                                         
     survey1      'Survey/wave (dummy)'                                                 
     lfdn         'Serial number (Cumulation)'                                          
     vlfdn        'Serial number (Pre-election Cross Section)'                          
     nlfdn        'Serial number (Post-election Cross Section)'                         
     intnum       'Number of interviewer'                                               
     welle        'Editing wave'                                                        
     intdate      'Date of interview'                                                   
     beginn       'Start time of interview'                                             
     ende         'End time of interview'                                               
     intdauer_ges 'Duration of interview in minutes'                                    
     bula         'Federal state (Bundesland)'                                          
     ostwest      'East/West (Berlin residents assigned to East)'                       
     ostwest2     'East/West (exact assignment of Berlin residents to East or West)'    
     vnvpoint     'Virtual sample point'                                                
     wahlkreis    'Electoral district'                                                  
     w_ow         'Weight, east/west'                                                   
     w_ipfges     'Weight of social/regional structure, total (with e/w weight)(MZ2017)'
     w_ipfost     'Weight of social/regional structure, east (MZ2017)'                  
     w_ipfwes     'Weight of social/regional structure, west (MZ2017))'                 
     vn_w_ipfges  'Weight of social/reg. structure, total (with e/w weight) cumulated'  
     vn_w_ipfost  'Weight of social/reg. structure, east cumulated'                     
     vn_w_ipfwes  'Weight of social/reg. structure, west cumulated'                     
     vn1          'Gender'                                                              
    
    In [3]:
    gles2017.vote <- subset(gles2017.sav,
                               select=c(
                                   survey = survey1,
                                   pre.turnout.int = v10,
                                   post.turnout = n10,
                                   pre.voteint.first = v11ab,
                                   pre.voteint.second = v11bb,
                                   post.vote.first = n11ab,
                                   post.vote.second = n11bb,
                                   pre.postvote.first = v12ab,
                                   pre.postvote.second = v12bb
                          ))
    codebook(gles2017.vote)
    
    ================================================================================
    
       survey 'Survey/wave (dummy)'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: -Inf - -1
    
       Values and labels         N Percent
                                          
       0   'Pre-election'     2179    50.8
       1   'Post-election'    2112    49.2
    
    ================================================================================
    
       pre.turnout.int 'Vote intention'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: -Inf - -1
    
       Values and labels                           N Valid Total
                                                                
       -98 M 'Don't know'                          7         0.2
       -97 M 'Not applicable'                     62         1.4
       -94 M 'Not in sampling frame'            2112        49.2
         1   'Certain to vote'                  1671  79.2  38.9
         2   'Likely to vote'                    151   7.2   3.5
         3   'Might vote'                         75   3.6   1.7
         4   'Not likely to vote'                 48   2.3   1.1
         5   'Certain not to vote'                70   3.3   1.6
         6   'Have already made a postal vote'    95   4.5   2.2
         7   'Not eligible to vote'                0   0.0   0.0
    
    ================================================================================
    
       post.turnout 'Turnout'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: -Inf - -1
    
       Values and labels                 N Valid Total
                                                      
       -99 M 'No answer'                 2         0.0
       -97 M 'Not applicable'           79         1.8
       -94 M 'Not in sampling frame'  2179        50.8
         1   'I did vote'             1826  89.9  42.6
         2   'I did not vote'          205  10.1   4.8
    
    ================================================================================
    
       pre.voteint.first 'Vote intention, first vote (version B)'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: -Inf - -1
    
       Values and labels                    N Valid Total
                                                         
       -99 M 'No answer'                   66         1.5
       -98 M 'Don't know'                 251         5.8
       -97 M 'Not applicable'             275         6.4
       -94 M 'Not in sampling frame'     2112        49.2
       -83 M 'Invalid vote'                14         0.3
         1   'CDU/CSU'                    693  44.1  16.2
         2   'CDU'                          0   0.0   0.0
         3   'CSU'                          0   0.0   0.0
         4   'SPD'                        383  24.3   8.9
         5   'FDP'                         90   5.7   2.1
         6   'GRUENE'                     133   8.5   3.1
         7   'DIE LINKE'                  143   9.1   3.3
       126   'BP'                           2   0.1   0.0
       149   'GRAUE'                        0   0.0   0.0
       151   'Die PARTEI'                   3   0.2   0.1
       152   'DIE VIOLETTEN'                1   0.1   0.0
       171   'FAMILIE'                      2   0.1   0.0
       180   'Freie Waehler'                4   0.3   0.1
       197   'KPD'                          0   0.0   0.0
       202   'MLPD'                         0   0.0   0.0
       206   'NPD'                          6   0.4   0.1
       207   'NSDAP'                        0   0.0   0.0
       209   'oedp'                         2   0.1   0.0
       214   'PBC'                          1   0.1   0.0
       215   'PIRATEN'                      4   0.3   0.1
       224   'RENTNER'                      1   0.1   0.0
       225   'REP'                          0   0.0   0.0
       226   'RRP'                          1   0.1   0.0
       229   'SED'                          0   0.0   0.0
       237   'Tierschutzpartei'             3   0.2   0.1
       241   'UDP'                          0   0.0   0.0
       249   'Volksabstimmung'              1   0.1   0.0
       304   'LDPD'                         0   0.0   0.0
       305   'DBD'                          0   0.0   0.0
       306   'DNVP'                         0   0.0   0.0
       307   'GB/BHE'                       0   0.0   0.0
       309   'NDPD'                         0   0.0   0.0
       322   'AfD'                         94   6.0   2.2
       323   'DIE RECHTE'                   0   0.0   0.0
       338   'BGE'                          2   0.1   0.0
       344   'V-Partei3'                    1   0.1   0.0
       348   'DM'                           0   0.0   0.0
       349   'DiB'                          0   0.0   0.0
       800   'Other'                        1   0.1   0.0
       801   'Other party'                  0   0.0   0.0
       802   'Individual candidate'         1   0.1   0.0
       803   'Voter community'              0   0.0   0.0
       808   'No party'                     1   0.1   0.0
       809   'All parties equally good'     0   0.0   0.0
       810   'With several parties'         0   0.0   0.0
    
    ================================================================================
    
       pre.voteint.second 'Vote intention, second vote (version B)'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: -Inf - -1
    
       Values and labels                    N Valid Total
                                                         
       -99 M 'No answer'                   63         1.5
       -98 M 'Don't know'                 236         5.5
       -97 M 'Not applicable'             275         6.4
       -94 M 'Not in sampling frame'     2112        49.2
       -83 M 'Invalid vote'                12         0.3
         1   'CDU/CSU'                    589  37.0  13.7
         2   'CDU'                          0   0.0   0.0
         3   'CSU'                          0   0.0   0.0
         4   'SPD'                        345  21.7   8.0
         5   'FDP'                        154   9.7   3.6
         6   'GRUENE'                     167  10.5   3.9
         7   'DIE LINKE'                  159  10.0   3.7
       126   'BP'                           2   0.1   0.0
       149   'GRAUE'                        0   0.0   0.0
       151   'Die PARTEI'                   9   0.6   0.2
       152   'DIE VIOLETTEN'                1   0.1   0.0
       171   'FAMILIE'                      2   0.1   0.0
       180   'Freie Waehler'                5   0.3   0.1
       197   'KPD'                          0   0.0   0.0
       202   'MLPD'                         0   0.0   0.0
       206   'NPD'                         11   0.7   0.3
       207   'NSDAP'                        0   0.0   0.0
       209   'oedp'                         2   0.1   0.0
       214   'PBC'                          1   0.1   0.0
       215   'PIRATEN'                     15   0.9   0.3
       224   'RENTNER'                      1   0.1   0.0
       225   'REP'                          0   0.0   0.0
       226   'RRP'                          1   0.1   0.0
       229   'SED'                          0   0.0   0.0
       237   'Tierschutzpartei'             5   0.3   0.1
       241   'UDP'                          0   0.0   0.0
       249   'Volksabstimmung'              1   0.1   0.0
       304   'LDPD'                         0   0.0   0.0
       305   'DBD'                          0   0.0   0.0
       306   'DNVP'                         0   0.0   0.0
       307   'GB/BHE'                       0   0.0   0.0
       309   'NDPD'                         0   0.0   0.0
       322   'AfD'                        116   7.3   2.7
       323   'DIE RECHTE'                   0   0.0   0.0
       338   'BGE'                          3   0.2   0.1
       344   'V-Partei3'                    0   0.0   0.0
       348   'DM'                           0   0.0   0.0
       349   'DiB'                          2   0.1   0.0
       800   'Other'                        1   0.1   0.0
       801   'Other party'                  0   0.0   0.0
       802   'Individual candidate'         0   0.0   0.0
       803   'Voter community'              0   0.0   0.0
       808   'No party'                     1   0.1   0.0
       809   'All parties equally good'     0   0.0   0.0
       810   'With several parties'         0   0.0   0.0
    
    ================================================================================
    
       post.vote.first 'Voting decision, first vote (version B)'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: -Inf - -1
    
       Values and labels                    N Valid Total
                                                         
       -99 M 'No answer'                  117         2.7
       -98 M 'Don't know'                  11         0.3
       -97 M 'Not applicable'             286         6.7
       -94 M 'Not in sampling frame'     2179        50.8
       -83 M 'Invalid vote'                11         0.3
         1   'CDU/CSU'                    594  35.2  13.8
         2   'CDU'                          0   0.0   0.0
         3   'CSU'                          0   0.0   0.0
         4   'SPD'                        425  25.2   9.9
         5   'FDP'                        136   8.1   3.2
         6   'GRUENE'                     163   9.7   3.8
         7   'DIE LINKE'                  167   9.9   3.9
       126   'BP'                           1   0.1   0.0
       149   'GRAUE'                        0   0.0   0.0
       151   'Die PARTEI'                  14   0.8   0.3
       152   'DIE VIOLETTEN'                0   0.0   0.0
       171   'FAMILIE'                      0   0.0   0.0
       180   'Freie Waehler'               14   0.8   0.3
       197   'KPD'                          0   0.0   0.0
       202   'MLPD'                         0   0.0   0.0
       206   'NPD'                          0   0.0   0.0
       207   'NSDAP'                        0   0.0   0.0
       209   'oedp'                         5   0.3   0.1
       214   'PBC'                          0   0.0   0.0
       215   'PIRATEN'                      7   0.4   0.2
       224   'RENTNER'                      0   0.0   0.0
       225   'REP'                          0   0.0   0.0
       226   'RRP'                          0   0.0   0.0
       229   'SED'                          0   0.0   0.0
       237   'Tierschutzpartei'             4   0.2   0.1
       241   'UDP'                          0   0.0   0.0
       249   'Volksabstimmung'              0   0.0   0.0
       304   'LDPD'                         0   0.0   0.0
       305   'DBD'                          1   0.1   0.0
       306   'DNVP'                         0   0.0   0.0
       307   'GB/BHE'                       0   0.0   0.0
       309   'NDPD'                         0   0.0   0.0
       322   'AfD'                        148   8.8   3.4
       323   'DIE RECHTE'                   0   0.0   0.0
       338   'BGE'                          0   0.0   0.0
       344   'V-Partei3'                    1   0.1   0.0
       348   'DM'                           0   0.0   0.0
       349   'DiB'                          0   0.0   0.0
       800   'Other'                        0   0.0   0.0
       801   'Other party'                  0   0.0   0.0
       802   'Individual candidate'         4   0.2   0.1
       803   'Voter community'              0   0.0   0.0
       808   'No party'                     0   0.0   0.0
       809   'All parties equally good'     0   0.0   0.0
       810   'With several parties'         0   0.0   0.0
             (unlab.val.)                   3   0.2   0.1
    
    ================================================================================
    
       post.vote.second 'Voting decision, second vote (version B)'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: -Inf - -1
    
       Values and labels                    N Valid Total
                                                         
       -99 M 'No answer'                  120         2.8
       -98 M 'Don't know'                   5         0.1
       -97 M 'Not applicable'             286         6.7
       -94 M 'Not in sampling frame'     2179        50.8
       -83 M 'Invalid vote'                11         0.3
         1   'CDU/CSU'                    504  29.8  11.7
         2   'CDU'                          0   0.0   0.0
         3   'CSU'                          0   0.0   0.0
         4   'SPD'                        338  20.0   7.9
         5   'FDP'                        200  11.8   4.7
         6   'GRUENE'                     225  13.3   5.2
         7   'DIE LINKE'                  188  11.1   4.4
       126   'BP'                           1   0.1   0.0
       149   'GRAUE'                        0   0.0   0.0
       151   'Die PARTEI'                  23   1.4   0.5
       152   'DIE VIOLETTEN'                0   0.0   0.0
       171   'FAMILIE'                      1   0.1   0.0
       180   'Freie Waehler'               17   1.0   0.4
       197   'KPD'                          1   0.1   0.0
       202   'MLPD'                         0   0.0   0.0
       206   'NPD'                          2   0.1   0.0
       207   'NSDAP'                        0   0.0   0.0
       209   'oedp'                         4   0.2   0.1
       214   'PBC'                          0   0.0   0.0
       215   'PIRATEN'                      8   0.5   0.2
       224   'RENTNER'                      1   0.1   0.0
       225   'REP'                          0   0.0   0.0
       226   'RRP'                          0   0.0   0.0
       229   'SED'                          0   0.0   0.0
       237   'Tierschutzpartei'             6   0.4   0.1
       241   'UDP'                          0   0.0   0.0
       249   'Volksabstimmung'              0   0.0   0.0
       304   'LDPD'                         0   0.0   0.0
       305   'DBD'                          0   0.0   0.0
       306   'DNVP'                         0   0.0   0.0
       307   'GB/BHE'                       0   0.0   0.0
       309   'NDPD'                         0   0.0   0.0
       322   'AfD'                        162   9.6   3.8
       323   'DIE RECHTE'                   0   0.0   0.0
       338   'BGE'                          2   0.1   0.0
       344   'V-Partei3'                    1   0.1   0.0
       348   'DM'                           0   0.0   0.0
       349   'DiB'                          2   0.1   0.0
       800   'Other'                        0   0.0   0.0
       801   'Other party'                  0   0.0   0.0
       802   'Individual candidate'         0   0.0   0.0
       803   'Voter community'              0   0.0   0.0
       808   'No party'                     0   0.0   0.0
       809   'All parties equally good'     0   0.0   0.0
       810   'With several parties'         0   0.0   0.0
             (unlab.val.)                   4   0.2   0.1
    
    ================================================================================
    
       pre.postvote.first 'Voting decision, absentee ballot, first vote (version
       B)'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: -Inf - -1
    
       Values and labels                    N Valid Total
                                                         
       -99 M 'No answer'                    5         0.1
       -98 M 'Don't know'                   1         0.0
       -97 M 'Not applicable'            2084        48.6
       -94 M 'Not in sampling frame'     2112        49.2
         1   'CDU/CSU'                     33  37.1   0.8
         2   'CDU'                          0   0.0   0.0
         3   'CSU'                          0   0.0   0.0
         4   'SPD'                         19  21.3   0.4
         5   'FDP'                          4   4.5   0.1
         6   'GRUENE'                      13  14.6   0.3
         7   'DIE LINKE'                   12  13.5   0.3
       126   'BP'                           0   0.0   0.0
       149   'GRAUE'                        0   0.0   0.0
       151   'Die PARTEI'                   1   1.1   0.0
       152   'DIE VIOLETTEN'                0   0.0   0.0
       171   'FAMILIE'                      0   0.0   0.0
       180   'Freie Waehler'                0   0.0   0.0
       197   'KPD'                          0   0.0   0.0
       202   'MLPD'                         0   0.0   0.0
       206   'NPD'                          1   1.1   0.0
       207   'NSDAP'                        0   0.0   0.0
       209   'oedp'                         1   1.1   0.0
       214   'PBC'                          1   1.1   0.0
       215   'PIRATEN'                      1   1.1   0.0
       224   'RENTNER'                      0   0.0   0.0
       225   'REP'                          0   0.0   0.0
       226   'RRP'                          0   0.0   0.0
       229   'SED'                          0   0.0   0.0
       237   'Tierschutzpartei'             0   0.0   0.0
       241   'UDP'                          0   0.0   0.0
       249   'Volksabstimmung'              0   0.0   0.0
       304   'LDPD'                         0   0.0   0.0
       305   'DBD'                          0   0.0   0.0
       306   'DNVP'                         0   0.0   0.0
       307   'GB/BHE'                       0   0.0   0.0
       309   'NDPD'                         0   0.0   0.0
       322   'AfD'                          3   3.4   0.1
       323   'DIE RECHTE'                   0   0.0   0.0
       338   'BGE'                          0   0.0   0.0
       344   'V-Partei3'                    0   0.0   0.0
       348   'DM'                           0   0.0   0.0
       349   'DiB'                          0   0.0   0.0
       800   'Other'                        0   0.0   0.0
       801   'Other party'                  0   0.0   0.0
       802   'Individual candidate'         0   0.0   0.0
       803   'Voter community'              0   0.0   0.0
       808   'No party'                     0   0.0   0.0
       809   'All parties equally good'     0   0.0   0.0
       810   'With several parties'         0   0.0   0.0
    
    ================================================================================
    
       pre.postvote.second 'Voting decision, absentee ballot, second vote (version
       B)'
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Missing values: -Inf - -1
    
       Values and labels                    N Valid Total
                                                         
       -99 M 'No answer'                    5         0.1
       -98 M 'Don't know'                   1         0.0
       -97 M 'Not applicable'            2084        48.6
       -94 M 'Not in sampling frame'     2112        49.2
         1   'CDU/CSU'                     27  30.3   0.6
         2   'CDU'                          0   0.0   0.0
         3   'CSU'                          0   0.0   0.0
         4   'SPD'                         20  22.5   0.5
         5   'FDP'                          8   9.0   0.2
         6   'GRUENE'                      11  12.4   0.3
         7   'DIE LINKE'                   12  13.5   0.3
       126   'BP'                           0   0.0   0.0
       149   'GRAUE'                        0   0.0   0.0
       151   'Die PARTEI'                   1   1.1   0.0
       152   'DIE VIOLETTEN'                0   0.0   0.0
       171   'FAMILIE'                      0   0.0   0.0
       180   'Freie Waehler'                1   1.1   0.0
       197   'KPD'                          0   0.0   0.0
       202   'MLPD'                         0   0.0   0.0
       206   'NPD'                          1   1.1   0.0
       207   'NSDAP'                        0   0.0   0.0
       209   'oedp'                         1   1.1   0.0
       214   'PBC'                          1   1.1   0.0
       215   'PIRATEN'                      2   2.2   0.0
       224   'RENTNER'                      0   0.0   0.0
       225   'REP'                          0   0.0   0.0
       226   'RRP'                          0   0.0   0.0
       229   'SED'                          0   0.0   0.0
       237   'Tierschutzpartei'             1   1.1   0.0
       241   'UDP'                          0   0.0   0.0
       249   'Volksabstimmung'              0   0.0   0.0
       304   'LDPD'                         0   0.0   0.0
       305   'DBD'                          0   0.0   0.0
       306   'DNVP'                         0   0.0   0.0
       307   'GB/BHE'                       0   0.0   0.0
       309   'NDPD'                         0   0.0   0.0
       322   'AfD'                          3   3.4   0.1
       323   'DIE RECHTE'                   0   0.0   0.0
       338   'BGE'                          0   0.0   0.0
       344   'V-Partei3'                    0   0.0   0.0
       348   'DM'                           0   0.0   0.0
       349   'DiB'                          0   0.0   0.0
       800   'Other'                        0   0.0   0.0
       801   'Other party'                  0   0.0   0.0
       802   'Individual candidate'         0   0.0   0.0
       803   'Voter community'              0   0.0   0.0
       808   'No party'                     0   0.0   0.0
       809   'All parties equally good'     0   0.0   0.0
       810   'With several parties'         0   0.0   0.0
    
    In [4]:
    gles2017.vote <- within(gles2017.vote,{
      vote.first <- cases(
                  survey == 0 & pre.turnout.int == 6 -> pre.postvote.first,
                  survey == 0 & pre.turnout.int %in% 4:5 -> -85,
                  survey == 0 & pre.turnout.int %in% 1:3 -> pre.voteint.first,
                  survey == 1 & post.turnout ==1 -> post.vote.first,
                  survey == 1 & post.turnout ==2 -> -85,
                  TRUE -> -97
                )
      vote.second <- cases(
                  survey == 0 & pre.turnout.int == 6 -> pre.postvote.second,
                  survey == 0 & pre.turnout.int %in% 4:5 -> -85,
                  survey == 0 & pre.turnout.int %in% 1:3 -> pre.voteint.second,
                  survey == 1 & post.turnout ==1 -> post.vote.second,
                  survey == 1 & post.turnout ==2 -> -85,
                  TRUE -> -97
      )
      vote.first <- as.item(vote.first, labels = labels(pre.postvote.first))
      vote.second <- as.item(vote.second, labels = labels(pre.postvote.second))
      valid.range(vote.first) <- valid.range(vote.second) <- c(1,900)
    })
    
    Warning message in cases(pre.postvote.first <- survey == 0 & pre.turnout.int == :
    “conditions are not mutually exclusive”
    Warning message in cases(pre.postvote.second <- survey == 0 & pre.turnout.int == :
    “conditions are not mutually exclusive”
    
    In [5]:
    codebook(gles2017.vote[c("vote.first","vote.second")])
    
    ================================================================================
    
       vote.first
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Valid range: 1 - 900
    
       Values and labels                    N Valid Total
                                                         
       -99 M 'No answer'                  187         4.4
       -98 M 'Don't know'                 263         6.1
       -97 M 'Not applicable'             150         3.5
       -85 M 'No turn out'                323         7.5
       -83 M 'Invalid vote'                25         0.6
         1   'CDU/CSU'                   1318  39.4  30.7
         2   'CDU'                          0   0.0   0.0
         3   'CSU'                          0   0.0   0.0
         4   'SPD'                        826  24.7  19.2
         5   'FDP'                        230   6.9   5.4
         6   'GRUENE'                     309   9.2   7.2
         7   'DIE LINKE'                  320   9.6   7.5
       126   'BP'                           3   0.1   0.1
       149   'GRAUE'                        0   0.0   0.0
       151   'Die PARTEI'                  18   0.5   0.4
       152   'DIE VIOLETTEN'                1   0.0   0.0
       171   'FAMILIE'                      2   0.1   0.0
       180   'Freie Waehler'               18   0.5   0.4
       197   'KPD'                          0   0.0   0.0
       202   'MLPD'                         0   0.0   0.0
       206   'NPD'                          7   0.2   0.2
       207   'NSDAP'                        0   0.0   0.0
       209   'oedp'                         8   0.2   0.2
       214   'PBC'                          2   0.1   0.0
       215   'PIRATEN'                     12   0.4   0.3
       224   'RENTNER'                      1   0.0   0.0
       225   'REP'                          0   0.0   0.0
       226   'RRP'                          1   0.0   0.0
       229   'SED'                          0   0.0   0.0
       237   'Tierschutzpartei'             7   0.2   0.2
       241   'UDP'                          0   0.0   0.0
       249   'Volksabstimmung'              1   0.0   0.0
       304   'LDPD'                         0   0.0   0.0
       305   'DBD'                          1   0.0   0.0
       306   'DNVP'                         0   0.0   0.0
       307   'GB/BHE'                       0   0.0   0.0
       309   'NDPD'                         0   0.0   0.0
       322   'AfD'                        244   7.3   5.7
       323   'DIE RECHTE'                   0   0.0   0.0
       338   'BGE'                          2   0.1   0.0
       344   'V-Partei3'                    2   0.1   0.0
       348   'DM'                           0   0.0   0.0
       349   'DiB'                          0   0.0   0.0
       800   'Other'                        1   0.0   0.0
       801   'Other party'                  0   0.0   0.0
       802   'Individual candidate'         5   0.1   0.1
       803   'Voter community'              0   0.0   0.0
       808   'No party'                     1   0.0   0.0
       809   'All parties equally good'     0   0.0   0.0
       810   'With several parties'         0   0.0   0.0
             (unlab.val.)                   3   0.1   0.1
    
    ================================================================================
    
       vote.second
    
    --------------------------------------------------------------------------------
    
       Storage mode: double
       Measurement: nominal
       Valid range: 1 - 900
    
       Values and labels                    N Valid Total
                                                         
       -99 M 'No answer'                  187         4.4
       -98 M 'Don't know'                 240         5.6
       -97 M 'Not applicable'             150         3.5
       -85 M 'No turn out'                323         7.5
       -83 M 'Invalid vote'                23         0.5
         1   'CDU/CSU'                   1119  33.2  26.1
         2   'CDU'                          0   0.0   0.0
         3   'CSU'                          0   0.0   0.0
         4   'SPD'                        702  20.8  16.4
         5   'FDP'                        362  10.7   8.4
         6   'GRUENE'                     403  12.0   9.4
         7   'DIE LINKE'                  358  10.6   8.3
       126   'BP'                           3   0.1   0.1
       149   'GRAUE'                        0   0.0   0.0
       151   'Die PARTEI'                  33   1.0   0.8
       152   'DIE VIOLETTEN'                1   0.0   0.0
       171   'FAMILIE'                      3   0.1   0.1
       180   'Freie Waehler'               23   0.7   0.5
       197   'KPD'                          1   0.0   0.0
       202   'MLPD'                         0   0.0   0.0
       206   'NPD'                         14   0.4   0.3
       207   'NSDAP'                        0   0.0   0.0
       209   'oedp'                         7   0.2   0.2
       214   'PBC'                          2   0.1   0.0
       215   'PIRATEN'                     25   0.7   0.6
       224   'RENTNER'                      2   0.1   0.0
       225   'REP'                          0   0.0   0.0
       226   'RRP'                          1   0.0   0.0
       229   'SED'                          0   0.0   0.0
       237   'Tierschutzpartei'            12   0.4   0.3
       241   'UDP'                          0   0.0   0.0
       249   'Volksabstimmung'              1   0.0   0.0
       304   'LDPD'                         0   0.0   0.0
       305   'DBD'                          0   0.0   0.0
       306   'DNVP'                         0   0.0   0.0
       307   'GB/BHE'                       0   0.0   0.0
       309   'NDPD'                         0   0.0   0.0
       322   'AfD'                        280   8.3   6.5
       323   'DIE RECHTE'                   0   0.0   0.0
       338   'BGE'                          5   0.1   0.1
       344   'V-Partei3'                    1   0.0   0.0
       348   'DM'                           0   0.0   0.0
       349   'DiB'                          4   0.1   0.1
       800   'Other'                        1   0.0   0.0
       801   'Other party'                  0   0.0   0.0
       802   'Individual candidate'         0   0.0   0.0
       803   'Voter community'              0   0.0   0.0
       808   'No party'                     1   0.0   0.0
       809   'All parties equally good'     0   0.0   0.0
       810   'With several parties'         0   0.0   0.0
             (unlab.val.)                   4   0.1   0.1
    
    In [ ]:
    
    
    In [ ]: