Working with a Crosswalk Table

library(retroharmonize)
library(dplyr)

Let’s read the bundled example surveys and create a metadata table. These steps are explained in the vignette Working With Survey Metadata.

The technical goal of survey harmonization is to create a single, tidy data frame containing unique observation identifiers and consistently named, coded and labelled variables.

examples_dir <- system.file("examples", package = "retroharmonize")
survey_files <- dir(examples_dir)[grepl("\\.rds", dir(examples_dir))]
survey_files
#> [1] "ZA5913.rds" "ZA6863.rds" "ZA7576.rds"
survey_paths <- file.path(examples_dir, survey_files)
example_metadata <- metadata_create(survey_paths = survey_paths)
#> Read: C:/Users/DanielAntal/AppData/Local/Temp/RtmpyKYQxw/Rinsta078584a3f07/retroharmonize/examples/ZA5913.rds
#> Read: C:/Users/DanielAntal/AppData/Local/Temp/RtmpyKYQxw/Rinsta078584a3f07/retroharmonize/examples/ZA6863.rds
#> Read: C:/Users/DanielAntal/AppData/Local/Temp/RtmpyKYQxw/Rinsta078584a3f07/retroharmonize/examples/ZA7576.rds

Crosswalk Table

A schema crosswalk, or crosswalk table, describes equivalent elements (or fields) across surveys. In this example, we create a crosswalk table for three variables: the unique observation identifier, trust in the European Parliament, and the country code.

We begin by selecting the relevant variables from our metadata table. By filtering out the other variables, we have the basic information in our metadata table.

subset_example_metadata <- example_metadata %>%
  filter(
    grepl(
      "^unique_identifier_in|trust|country_code",
      var_label_orig
    )
  ) %>%
  filter(
    grepl(
      "^unique_identifier_in|european_parliament|country_code",
      var_label_orig
    )
  ) %>%
  filter(var_name_orig != "uniqid_orig")

subset_example_metadata
#>     filename     id var_name_orig          class_orig
#> 1 ZA5913.rds ZA5913         rowid           character
#> 2 ZA5913.rds ZA5913      isocntry           character
#> 3 ZA5913.rds ZA5913        qa10_1 haven_labelled_spss
#> 4 ZA6863.rds ZA6863         rowid           character
#> 5 ZA6863.rds ZA6863      isocntry           character
#> 6 ZA6863.rds ZA6863        qa14_1      haven_labelled
#> 7 ZA7576.rds ZA7576         rowid           character
#> 8 ZA7576.rds ZA7576      isocntry           character
#> 9 ZA7576.rds ZA7576        qa14_1 haven_labelled_spss
#>                 var_label_orig     labels valid_labels na_labels na_range
#> 1 unique_identifier_in_za_5913         NA           NA        NA       NA
#> 2        country_code_iso_3166         NA           NA        NA       NA
#> 3    european_parliament_trust    1, 2, 3         1, 2         3       NA
#> 4 unique_identifier_in_za_6863         NA           NA        NA       NA
#> 5        country_code_iso_3166         NA           NA        NA       NA
#> 6    european_parliament_trust    1, 2, 3      1, 2, 3                 NA
#> 7 unique_identifier_in_za_7576         NA           NA        NA       NA
#> 8        country_code_iso_3166         NA           NA        NA       NA
#> 9    european_parliament_trust 1, 2, 3, 9      1, 2, 3         9       NA
#>   n_labels n_valid_labels n_na_labels
#> 1        0              0           0
#> 2        0              0           0
#> 3        3              2           1
#> 4        0              0           0
#> 5        0              0           0
#> 6        3              3           0
#> 7        0              0           0
#> 8        0              0           0
#> 9        4              3           1

Create a crosswalk table

You can create a crosswalk table with crosswalk_table_create() and validate it with is.crosswalk_table(). Crosswalk tables can also be created or edited in spreadsheet applications. The mandatory columns are:

Optional columns specify how value codes, value labels, missing values and R data types should be harmonized.

ct <- crosswalk_table_create(subset_example_metadata)
ct
#> # A tibble: 16 × 16
#>    id     filename   var_name_orig var_name_target val_numeric_orig
#>    <chr>  <chr>      <chr>         <chr>                      <dbl>
#>  1 ZA5913 ZA5913.rds rowid         rowid                         NA
#>  2 ZA5913 ZA5913.rds isocntry      isocntry                      NA
#>  3 ZA5913 ZA5913.rds qa10_1        qa10_1                         1
#>  4 ZA5913 ZA5913.rds qa10_1        qa10_1                         2
#>  5 ZA5913 ZA5913.rds qa10_1        qa10_1                         3
#>  6 ZA6863 ZA6863.rds rowid         rowid                         NA
#>  7 ZA6863 ZA6863.rds isocntry      isocntry                      NA
#>  8 ZA6863 ZA6863.rds qa14_1        qa14_1                         1
#>  9 ZA6863 ZA6863.rds qa14_1        qa14_1                         2
#> 10 ZA6863 ZA6863.rds qa14_1        qa14_1                         3
#> 11 ZA7576 ZA7576.rds rowid         rowid                         NA
#> 12 ZA7576 ZA7576.rds isocntry      isocntry                      NA
#> 13 ZA7576 ZA7576.rds qa14_1        qa14_1                         1
#> 14 ZA7576 ZA7576.rds qa14_1        qa14_1                         2
#> 15 ZA7576 ZA7576.rds qa14_1        qa14_1                         3
#> 16 ZA7576 ZA7576.rds qa14_1        qa14_1                         9
#> # ℹ 11 more variables: val_numeric_target <dbl>, val_label_orig <chr>,
#> #   val_label_target <chr>, class_orig <chr>, class_target <chr>,
#> #   na_label_orig <chr>, na_label_target <chr>, na_numeric_orig <dbl>,
#> #   na_numeric_target <dbl>, var_label_orig <chr>, var_label_target <chr>

Variable name harmonization

Variable name harmonization assigns a single, unambiguous name to variables representing the same concept.

ct %>%
  mutate(var_name_target = case_when(
    var_name_orig == "rowid" ~ .data$var_name_orig,
    var_name_orig == "isocntry" ~ "geo",
    TRUE ~ "trust_ep"
  )) %>%
  distinct(across(all_of(c("filename", "var_name_orig", "var_name_target"))))
#> # A tibble: 9 × 3
#>   filename   var_name_orig var_name_target
#>   <chr>      <chr>         <chr>          
#> 1 ZA5913.rds rowid         rowid          
#> 2 ZA5913.rds isocntry      geo            
#> 3 ZA5913.rds qa10_1        trust_ep       
#> 4 ZA6863.rds rowid         rowid          
#> 5 ZA6863.rds isocntry      geo            
#> 6 ZA6863.rds qa14_1        trust_ep       
#> 7 ZA7576.rds rowid         rowid          
#> 8 ZA7576.rds isocntry      geo            
#> 9 ZA7576.rds qa14_1        trust_ep

Value numeric code and label harmonization

For value harmonization, the crosswalk table contains instructions for recoding and relabelling survey responses.

By default, crosswalk_table_create() assigns identical source and target values. You can export the table to a spreadsheet and modify the target columns. Unchanged target values retain their original coding.

To ensure reproducibility, we define the target coding programmatically. Special missing-value codes are placed outside the range of valid responses.

ct %>%
  mutate(val_numeric_target = case_when(
    val_numeric_orig == 1 ~ .data$val_numeric_orig,
    val_numeric_orig == 2 ~ 0,
    TRUE ~ 99999
  )) %>%
  mutate(val_label_target = case_when(
    val_numeric_orig == 1 ~ "trust",
    val_numeric_orig == 2 ~ "distrust",
    TRUE ~ "declined"
  )) %>%
  distinct(across(all_of(c(
    "filename",
    "val_numeric_orig", "val_numeric_target",
    "val_label_orig", "val_label_target"
  ))))
#> # A tibble: 13 × 5
#>    filename  val_numeric_orig val_numeric_target val_label_orig val_label_target
#>    <chr>                <dbl>              <dbl> <chr>          <chr>           
#>  1 ZA5913.r…               NA              99999 <NA>           declined        
#>  2 ZA5913.r…                1                  1 Tend to trust  trust           
#>  3 ZA5913.r…                2                  0 Tend not to t… distrust        
#>  4 ZA5913.r…                3              99999 DK             declined        
#>  5 ZA6863.r…               NA              99999 <NA>           declined        
#>  6 ZA6863.r…                1                  1 Tend to trust  trust           
#>  7 ZA6863.r…                2                  0 Tend not to t… distrust        
#>  8 ZA6863.r…                3              99999 DK             declined        
#>  9 ZA7576.r…               NA              99999 <NA>           declined        
#> 10 ZA7576.r…                1                  1 Tend to trust  trust           
#> 11 ZA7576.r…                2                  0 Tend not to t… distrust        
#> 12 ZA7576.r…                3              99999 DK             declined        
#> 13 ZA7576.r…                9              99999 Inap. (not CY… declined

Special values require particular attention. They often represent missing responses, declined answers or questionnaire items that were not administered.

ct %>%
  mutate(na_numeric_target = case_when(
    na_numeric_orig == 3 ~ 99999,
    TRUE ~ NA_real_
  )) %>%
  mutate(na_label_target = case_when(
    na_numeric_target == 99999 ~ "declined",
    TRUE ~ NA_character_
  )) %>%
  distinct(across(all_of(c(
    "filename", "val_numeric_orig",
    "na_numeric_orig", "na_numeric_target",
    "na_label_orig", "na_label_target"
  ))))
#> # A tibble: 13 × 6
#>    filename   val_numeric_orig na_numeric_orig na_numeric_target na_label_orig
#>    <chr>                 <dbl>           <dbl>             <dbl> <chr>        
#>  1 ZA5913.rds               NA              NA                NA <NA>         
#>  2 ZA5913.rds                1              NA                NA <NA>         
#>  3 ZA5913.rds                2              NA                NA <NA>         
#>  4 ZA5913.rds                3              NA                NA <NA>         
#>  5 ZA6863.rds               NA              NA                NA <NA>         
#>  6 ZA6863.rds                1              NA                NA <NA>         
#>  7 ZA6863.rds                2              NA                NA <NA>         
#>  8 ZA6863.rds                3              NA                NA <NA>         
#>  9 ZA7576.rds               NA              NA                NA <NA>         
#> 10 ZA7576.rds                1              NA                NA <NA>         
#> 11 ZA7576.rds                2              NA                NA <NA>         
#> 12 ZA7576.rds                3              NA                NA <NA>         
#> 13 ZA7576.rds                9              NA                NA <NA>         
#> # ℹ 1 more variable: na_label_target <chr>

A reproducible recoding and relabelling

We can combine these transformations into a single pipeline using dplyr:

example_crosswalk_table <- ct %>%
  mutate(var_name_target = case_when(
    var_name_orig == "rowid" ~ .data$var_name_orig,
    var_name_orig == "isocntry" ~ "geo",
    TRUE ~ "trust_ep"
  )) %>%
  mutate(val_numeric_target = case_when(
    val_numeric_orig == 1 ~ .data$val_numeric_orig,
    val_numeric_orig == 2 ~ 0,
    TRUE ~ 99999
  )) %>%
  mutate(val_label_target = case_when(
    val_numeric_orig == 1 ~ "trust",
    val_numeric_orig == 2 ~ "distrust",
    TRUE ~ "declined"
  )) %>%
  mutate(na_numeric_target = case_when(
    na_numeric_orig == 3 ~ 99999,
    TRUE ~ NA_real_
  )) %>%
  mutate(na_label_target = case_when(
    na_numeric_target == 99999 ~ "declined",
    TRUE ~ NA_character_
  ))

example_crosswalk_table
#> # A tibble: 16 × 16
#>    id     filename   var_name_orig var_name_target val_numeric_orig
#>    <chr>  <chr>      <chr>         <chr>                      <dbl>
#>  1 ZA5913 ZA5913.rds rowid         rowid                         NA
#>  2 ZA5913 ZA5913.rds isocntry      geo                           NA
#>  3 ZA5913 ZA5913.rds qa10_1        trust_ep                       1
#>  4 ZA5913 ZA5913.rds qa10_1        trust_ep                       2
#>  5 ZA5913 ZA5913.rds qa10_1        trust_ep                       3
#>  6 ZA6863 ZA6863.rds rowid         rowid                         NA
#>  7 ZA6863 ZA6863.rds isocntry      geo                           NA
#>  8 ZA6863 ZA6863.rds qa14_1        trust_ep                       1
#>  9 ZA6863 ZA6863.rds qa14_1        trust_ep                       2
#> 10 ZA6863 ZA6863.rds qa14_1        trust_ep                       3
#> 11 ZA7576 ZA7576.rds rowid         rowid                         NA
#> 12 ZA7576 ZA7576.rds isocntry      geo                           NA
#> 13 ZA7576 ZA7576.rds qa14_1        trust_ep                       1
#> 14 ZA7576 ZA7576.rds qa14_1        trust_ep                       2
#> 15 ZA7576 ZA7576.rds qa14_1        trust_ep                       3
#> 16 ZA7576 ZA7576.rds qa14_1        trust_ep                       9
#> # ℹ 11 more variables: val_numeric_target <dbl>, val_label_orig <chr>,
#> #   val_label_target <chr>, class_orig <chr>, class_target <chr>,
#> #   na_label_orig <chr>, na_label_target <chr>, na_numeric_orig <dbl>,
#> #   na_numeric_target <dbl>, var_label_orig <chr>, var_label_target <chr>

Using dplyr is optional. The crosswalk table can also be edited manually or generated using other tools.

Reading the crosswalk table is very simple, for example, the last two (15-16) rows read like this:

Subsetting

Removing variables that will not be harmonized simplifies subsequent processing and reduces memory use.

Smaller collections of surveys can be processed entirely in memory. Larger collections can be imported and subsetted sequentially, reducing memory requirements.

From a technical perspective, the aim of the survey harmonization is to create a single, tidy, joined table. For making joining possible (and to reduce memory use), a first processing step is to remove irrelevant variables that will not be harmonized.

There are several ways how the subsetting can be made. With smaller tasks all the surveys can be stored in memory and the subsequent processing made fast in memory. With many surveys, we provide a slower but memory-saving way of importing and subsetting consecutive surveys from files.

Recall from the vignette Working With Survey Metadata that you can read files into a list of surveys:

example_surveys <- read_surveys(survey_paths, .f = "read_rds")

We now retain only the variables relevant to our harmonization task.

subset_survey_list_1 <- subset_surveys(
  survey_list = example_surveys,
  subset_vars = c("rowid", "isocntry", "qa10_1", "qa14_1"),
  subset_name = "subset_example"
)

We still have the three surveys in a list:

vapply(subset_survey_list_1, function(x) attr(x, "id"), character(1))
#> [1] "ZA5913" "ZA6863" "ZA7576"

The top few rows of the first subsetted survey to see if the subsetting took place:

head(subset_survey_list_1[[1]])
#> Unknown (2026): Untitled Dataset [dataset]
#>   rowid    isocntry qa10_1                
#>   <chr>    <chr>    <dbl+lbl>            
#> 1 ZA5913_1 NL       2 [Tend not to trust]
#> 2 ZA5913_2 NL       2 [Tend not to trust]
#> 3 ZA5913_3 NL       3 (NA) [DK]          
#> 4 ZA5913_4 NL       1 [Tend to trust]    
#> 5 ZA5913_5 NL       1 [Tend to trust]    
#> 6 ZA5913_6 NL       1 [Tend to trust]

Although qa10_1 and qa14_1 represent trust in the European Parliament, their different names prevent straightforward concatenation.

lapply(subset_survey_list_1, names)
#> [[1]]
#> [1] "rowid"    "isocntry" "qa10_1"  
#> 
#> [[2]]
#> [1] "rowid"    "isocntry" "qa14_1"  
#> 
#> [[3]]
#> [1] "rowid"    "isocntry" "qa14_1"

The next step is to harmonize the names of those variables that represent the same concept, in this case, Trust in the European Parliament. The next steps, i.e. the harmonization of the numerical codes of answers and their labels will be discussed in the Harmonize Value Labels vignette.

Variable Name Harmonization

It is very practical to do the subsetting and the variable name harmonization in one step. The subset_save_surveys() function will do this optionally, and a wrapper function harmonize_surveys() will validate that all metadata (i.e. the original, source variable names and the new, target variable names) are present.

subset_survey_list_2 <- subset_surveys(
  crosswalk_table = example_crosswalk_table,
  survey_list = example_surveys,
  subset_name = "trust_ep"
)

We have again the three surveys in a list:

vapply(subset_survey_list_2, function(x) attr(x, "id"), character(1))
#> [1] "ZA5913" "ZA6863" "ZA7576"

The top few rows of the first subsetted survey now show that we have new, harmonized names.

head(subset_survey_list_2[[1]])
#> Unknown (2026): Untitled Dataset [dataset]
#>   rowid    isocntry qa10_1                
#>   <chr>    <chr>    <dbl+lbl>            
#> 1 ZA5913_1 NL       2 [Tend not to trust]
#> 2 ZA5913_2 NL       2 [Tend not to trust]
#> 3 ZA5913_3 NL       3 (NA) [DK]          
#> 4 ZA5913_4 NL       1 [Tend to trust]    
#> 5 ZA5913_5 NL       1 [Tend to trust]    
#> 6 ZA5913_6 NL       1 [Tend to trust]

Our variable names are harmonized:

lapply(subset_survey_list_2, names)
#> [[1]]
#> [1] "rowid"    "isocntry" "qa10_1"  
#> 
#> [[2]]
#> [1] "rowid"    "isocntry" "qa14_1"  
#> 
#> [[3]]
#> [1] "rowid"    "isocntry" "qa14_1"

Although these examples fit easily into memory, larger survey collections may benefit from sequential file processing. The import_path argument specifies the source directory, and export_path specifies the destination directory.

Without an export path, subset_surveys() returns a list of surveys. With an export path, it saves the subsetted surveys and returns their filenames.

Subsetting can use either a vector of variable names or a crosswalk table. See ?subset_surveys for further details.

subset_surveys(
  survey_list = example_surveys,
  crosswalk_table = example_crosswalk_table,
  subset_name = "trust_ep",
  import_path = examples_dir,
  export_path = tempdir()
)
#> Saving ZA5913_trust_ep.rds
#> Saving ZA6863_trust_ep.rds
#> Saving ZA7576_trust_ep.rds
#> [1] "ZA5913_trust_ep.rds" "ZA6863_trust_ep.rds" "ZA7576_trust_ep.rds"

The subsetted surveys are saved with a common name element, trust_ep to the export file location, in this case a temporary directory created with tempdir().

readRDS(file.path(tempdir(), "ZA5913_trust_ep.rds")) %>%
  head()
#> Unknown (2026): Untitled Dataset [dataset]
#>   rowid    isocntry qa10_1                
#>   <chr>    <chr>    <dbl+lbl>            
#> 1 ZA5913_1 NL       2 [Tend not to trust]
#> 2 ZA5913_2 NL       2 [Tend not to trust]
#> 3 ZA5913_3 NL       3 (NA) [DK]          
#> 4 ZA5913_4 NL       1 [Tend to trust]    
#> 5 ZA5913_5 NL       1 [Tend to trust]    
#> 6 ZA5913_6 NL       1 [Tend to trust]

Further Steps

Surveys with harmonized variable names are not necessarily ready for statistical analysis. Value codes, value labels and missing-value definitions must also be made consistent.

The labelled_spss_survey class preserves coding metadata throughout harmonization. Its conversion methods, as_numeric(), as_factor() and as_character(), take this metadata into account.

For example, if 999 represents a declined answer, base R’s as.numeric() retains the numeric value 999, whereas as_numeric() converts it to NA_real_. Ignoring this distinction can produce misleading statistical results.

Crosswalk tables provide a reproducible specification for value harmonization and type conversion, as demonstrated in Harmonize Value Labels vignette.