When working with survey data there are several issues / strategies to clean and prepare the data that are useful and worth being incorporated to the routines and workflow. This vignette uses the CEOdata
package to present several examples.
It uses primarily the data retrieved by default using the CEOdata()
function in its default form, which retrieves the compiled “Barometers” from 2014 onwards.
library(CEOdata)
CEOdata() d <-
Once you have retrieved the data of the surveys, it is easy to accommodate them to your regular workflow. For instance, to get the overall number of males and females surveyed:
library(dplyr)
library(tidyr)
library(ggplot2)
|>
d count(SEXE)
## # A tibble: 2 × 2
## SEXE n
## <fct> <int>
## 1 Home 17269
## 2 Dona 18569
Or to trace the proportion of females surveyed over time, across barometers:
|>
d group_by(BOP_NUM) |>
summarize(propFemales = length(which(SEXE == "Dona")) / n()) |>
ggplot(aes(x = BOP_NUM, y = propFemales, group = 1)) +
geom_point() +
geom_line() +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) +
expand_limits(y = c(0, 1))
The metadata also provides the option of examining the time periods where there has been fieldwork in quantitative studies, since 2018. In addition, we can distinguish between studies that provide microdata and surveys that don’t.
CEOmeta() |>
filter(`Dia inici treball de camp` > "2018-01-01") |>
ggplot(aes(xmin = `Dia inici treball de camp`,
xmax = `Dia final treball de camp`,
y = reorder(REO, `Dia final treball de camp`),
color = microdata_available)) +
geom_linerange() +
xlab("Date") + ylab("Surveys with fieldwork") +
theme(axis.ticks.y = element_blank(), axis.text.y = element_blank())
Once a dataset has been retrieved from the CEO servers, it is important to clean it and arrange it to one’s individual preferences, and store the result in an R object.
The following example, for instance, process several variables of the survey, picks them and stores the resulting object in a workspace (RData) format.
d |>
survey.data <- mutate(Female = ifelse(SEXE == "Dona", 1, 0),
Age = EDAT,
# Pass NA correctly
Income = ifelse(INGRESSOS_1_15 %in% c("No ho sap", "No contesta"),
NA,
1_15),
INGRESSOS_Date = Data,
# Reorganize factor labels
`Place of birth` = factor(case_when(
== "Catalunya" ~ "Catalonia",
LLOC_NAIX %in% c("No ho sap", "No contesta") ~ as.character(NA),
LLOC_NAIX TRUE ~ "Outside Catalonia")),
# Convert into numerical (integer)
`Interest in politics` = case_when(
== "Gens" ~ 0L,
INTERES_POL == "Poc" ~ 1L,
INTERES_POL == "Bastant" ~ 2L,
INTERES_POL == "Molt" ~ 3L,
INTERES_POL TRUE ~ as.integer(NA)),
# Convert into numeric (double) and properly address missing values
`Satisfaction with democracy` = ifelse(
%in% c("No ho sap", "No contesta"),
SATIS_DEMOCRACIA NA,
as.numeric(SATIS_DEMOCRACIA))) |>
# Center income to the median
mutate(Income = Income - median(Income, na.rm = TRUE)) |>
# Pick only specific variables
select(Date, Female, Age, Income,
`Place of birth`, `Interest in politics`,
`Satisfaction with democracy`)
Finally, this can be stored for further analysis (hence, without the need to download and arrange the data again) in an R’s native format:
save(survey.data, file = "my_cleaned_dataset.RData")
There are several packages that construct convenient tables with the descriptive summary of a dataset. For example, using the vtable
package to produce a table with descriptive statistics.
library(vtable)
st(survey.data)
Variable | N | Mean | Std. Dev. | Min | Pctl. 25 | Pctl. 75 | Max |
---|---|---|---|---|---|---|---|
Female | 35838 | 0.518 | 0.5 | 0 | 0 | 1 | 1 |
Age | 35838 | 50.354 | 17.713 | 18 | 36 | 65 | 99 |
Income | 26500 | -0.168 | 2.778 | -7 | -2 | 2 | 7 |
Place of birth | 35836 | ||||||
… Catalonia | 25205 | 70.3% | |||||
… Outside Catalonia | 10631 | 29.7% | |||||
Interest in politics | 33736 | 1.465 | 0.976 | 0 | 1 | 2 | 3 |
Satisfaction with democracy | 35089 | 3.039 | 0.734 | 1 | 3 | 4 | 4 |
Or the compareGroups
that allows to flexibly produce tables that compare descriptive statistics for different groups of individuals.
library(compareGroups)
createTable(compareGroups(Female ~ . -Date, data = survey.data))
##
## --------Summary descriptives table by 'Female'---------
##
## _________________________________________________________________
## 0 1 p.overall
## N=17269 N=18569
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
## Edat 49.3 (17.6) 51.3 (17.8) <0.001
## Income 0.16 (2.76) -0.49 (2.75) <0.001
## Place of birth: <0.001
## Catalonia 12469 (72.2%) 12736 (68.6%)
## Outside Catalonia 4798 (27.8%) 5833 (31.4%)
## Interest in politics 1.59 (0.97) 1.35 (0.97) <0.001
## Satisfaction with democracy 3.03 (0.75) 3.05 (0.72) 0.002
## ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
The development of CEOdata
(track changes, propose improvements, report bugs) can be followed at github.
If using the data and the package, please cite and acknowledge properly the CEO and the package, respectively.