knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(retroharmonize) library(dplyr)
examples_dir <- system.file("examples", package = "retroharmonize") survey_files <- dir(examples_dir)[grepl("\\.rds", dir(examples_dir))] survey_files
survey_1 <- read_rds(file.path(examples_dir, survey_files[1]))
This function should be renamed and slightly rewritten, it does too many things.
metadata_create(survey_1) %>% head()
survey_paths <- file.path(examples_dir, survey_files)
With smaller data frames representing your surveys, the most efficient way to work with the information is to read them into a list of surveys.
Read the surveys into a list object in the memory:
example_surveys <- read_surveys(survey_paths, .f = "read_rds")
Map the metadata contents of the files:
set.seed(2022) metadata_create(survey_list = example_surveys) %>% sample_n(12)
If you may ran out of memory, you can work with files. The advantage of keeping the surveys in memory is that later it will be much faster to continue working with them, but from the metadata point of view, the returned object is the same either way.
example_metadata <- metadata_create ( survey_paths = survey_paths, .f = "read_rds")
set.seed(2022) example_metadata %>% sample_n(12)
A quick glance at some metadata:
library(dplyr) subset_example_metadata <- example_metadata %>% filter ( grepl("trust", .data$var_label_orig) ) %>% filter ( grepl("european_parliament", .data$var_label_orig)) %>% select ( all_of(c("filename", "var_label_orig", "var_name_orig", "valid_labels", "na_labels", "class_orig"))) subset_example_metadata
In ZA5913.rds
the Trust in European Parliament variable is called qa10_1
, in the other surveys it is called qa14_1
.
In the first survey, the variable has two values (coded as 1 and 2, and labelled as Tend to trust
and Tend not to trust
. )
unlist(subset_example_metadata$valid_labels[1])
In the first survey, the variable has two values (coded as 1 and 2, and labelled as Tend to trust
and Tend not to trust
.) In the second survey, we have three values, and non of them are marked as special, missing values. This is not surprising, because they were not SPSS files. They have related, but not exactly matching classes, too. Therefore, these variables need to be harmonized.
unlist(subset_example_metadata$valid_labels[2]) unlist(subset_example_metadata$na_labels[2])
The metadata created by the metadata_create()
and its version for multiple surveys, metadata_create
, gives a first overview for the harmonization of concepts, the necessary harmonization of variable names and variable labels. In this case:
qa10_1
and qa14_1
, for example, trust_european_parliament
, because the variable refers to the same concept.Declined
(to answer) is missing from ZA5913.rds
.qa10_1
and qa14_1
into a numerical variable or factor variable. It is practical to use 1="Tend to Trust", 0="Tend not to trust" for calculating the percentage of people trusting the European Parliament, making sure that Decline will get a NA_real_
value for averaging, or creating a factor variable with three levels, for example trust, not_trust, declined.Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.