Skip to contents

Harmonize the value codes and value labels across multiple surveys.

Usage

harmonize_survey_values(survey_list, .f, status_message = FALSE)

harmonize_waves(waves, .f, status_message = FALSE)

Arguments

survey_list

A list of surveys. In the deprecated form the parameter was called waves.

.f

A function to apply for the harmonization.

status_message

Defaults to FALSE. If set to TRUE it shows the id of the survey that is being joined.

waves

A list of surveys. Deprecated.

Value

A natural full join of all surveys in a single data frame.

Details

The functions binds together variables that are all present in the surveys, and applies a harmonization function .f on them. Till retroharmonize 0.2.0 called harmonize_waves.

The earlier form harmonize_waves is deprecated. The function is currently called harmonize_waves.

Examples

# \donttest{
examples_dir <- system.file("examples", package = "retroharmonize")
survey_list <- dir(examples_dir)[grepl("\\.rds", dir(examples_dir))]

example_surveys <- read_surveys(
  file.path( examples_dir, survey_list), 
  save_to_rds = FALSE)
#> Error in read_surveys(file.path(examples_dir, survey_list), save_to_rds = FALSE): unused argument (save_to_rds = FALSE)

metadata <- lapply ( X = example_surveys, FUN = metadata_create )
#> Error in lapply(X = example_surveys, FUN = metadata_create): object 'example_surveys' not found
metadata <- do.call(rbind, metadata)
#> Error in do.call(rbind, metadata): object 'metadata' not found

require(dplyr)
#> Loading required package: dplyr
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union

to_harmonize <- metadata %>%
  filter ( var_name_orig %in% 
             c("rowid", "w1") |
             grepl("^trust", var_label_orig ) ) %>%
  mutate ( var_label = var_label_normalize(var_label_orig) ) %>%
  mutate ( var_name_target = val_label_normalize(var_label_orig) ) %>%
  mutate ( var_name_target = ifelse(.data$var_name_orig %in% c("rowid", "w1", "wex"), 
                                    .data$var_name_orig, .data$var_name_target) )
#> Error in filter(., var_name_orig %in% c("rowid", "w1") | grepl("^trust",     var_label_orig)): object 'metadata' not found

harmonize_eb_trust <- function(x) {
  label_list <- list(
    from = c("^tend\\snot", "^cannot", "^tend\\sto", "^can\\srely",
             "^dk", "^inap", "na"), 
   to = c("not_trust", "not_trust", "trust", "trust",
           "do_not_know", "inap", "inap"), 
    numeric_values = c(0,0,1,1, 99997,99999,99999)
  )
  
  harmonize_survey_values(x, 
                   harmonize_labels = label_list, 
                   na_values = c("do_not_know"=99997,
                                 "declined"=99998,
                                 "inap"=99999)
                   )
}

merged_surveys <- merge_surveys ( example_surveys, var_harmonization = to_harmonize  )
#> Error in eval(assertion, env): object 'example_surveys' not found

harmonized <- harmonize_survey_values(survey_list = merged_surveys, 
                              .f = harmonize_eb_trust,
                              status_message = FALSE)
#> Error in eval(assertion, env): object 'merged_surveys' not found
                              
# For details see Afrobarometer and Eurobarometer Case Study vignettes.
# }