| Title: | Search and Examine Variables Across Survey Datasets |
| Version: | 0.1.0 |
| Description: | Search for variables across multiple survey datasets, examine variable properties (labels, values, missingness), and explore variable context within datasets. Useful for navigating complex survey data with many variables and understanding variable relationships and metadata. |
| License: | MIT + file LICENSE |
| Encoding: | UTF-8 |
| RoxygenNote: | 7.3.3 |
| Suggests: | testthat (≥ 3.0.0) |
| NeedsCompilation: | no |
| Packaged: | 2026-05-04 09:57:24 UTC; malor |
| Author: | Malo Raballand [aut, cre] |
| Maintainer: | Malo Raballand <malo.raballand@sciencespo.fr> |
| Repository: | CRAN |
| Date/Publication: | 2026-05-07 15:51:26 UTC |
Examine a Variable in Detail
Description
Display detailed information about a specific variable including its label, class, missing values, value labels, and summary statistics.
Usage
examine_variable(var_name, data, verbose = TRUE)
Arguments
var_name |
Character string. Name of the variable to examine. |
data |
Data frame containing the variable. |
verbose |
Logical. If TRUE, print detailed information to console. |
Value
Invisibly returns a list with elements:
- name
Variable name
- label
Variable label
- class
Variable class
- n_unique
Number of unique non-missing values
- n_missing
Number of missing values
Examples
df <- data.frame(age = c(25, 30, 35))
attr(df$age, "label") <- "Age in years"
examine_variable("age", data = df)
Search for Variables Across Datasets
Description
Search for variables matching a pattern across multiple datasets. Searches both variable names and variable labels.
Usage
search_variables(pattern, data_list, datasets_info = NULL)
Arguments
pattern |
A regular expression or literal string to match (case-insensitive). |
data_list |
A named list of data frames to search. |
datasets_info |
Optional data frame with column |
Value
Invisibly returns a data frame with columns:
- dataset
Name of the dataset
- variable
Variable name
- label
Variable label or "No label" if missing
- match_type
Either "Variable Name" or "Label Text"
Examples
# Create sample data with labels
df1 <- data.frame(age = 1:3, income = c(50000, 60000, 70000))
attr(df1$age, "label") <- "Age in years"
attr(df1$income, "label") <- "Annual income"
df2 <- data.frame(education = c("HS", "BA", "MA"), employment = c("Yes", "No", "Yes"))
attr(df2$education, "label") <- "Education level"
attr(df2$employment, "label") <- "Currently employed"
# Create a named list of data frames (NOT a list of text names!)
my_datasets <- list(survey_2023 = df1, survey_2024 = df2)
# Search across multiple datasets
search_variables("age", data_list = my_datasets)
# Search for pattern in labels
search_variables("income", data_list = my_datasets)
# WRONG - do not do this:
# wrong_list <- list(c("survey_2023", "survey_2024")) # List of text!
# search_variables("age", data_list = wrong_list) # Will fail!
Show Variable Context in Dataset
Description
Display a variable along with specified number of variables before and after it in dataset order.
Usage
show_variable_context(var_name, data, before = 5, after = 5, verbose = TRUE)
Arguments
var_name |
Character string. Name of the variable to show context for. |
data |
Data frame containing the variable. |
before |
Integer. Number of variables to show before the target variable. Default is 5. |
after |
Integer. Number of variables to show after the target variable. Default is 5. |
verbose |
Logical. If TRUE, print formatted output. |
Value
Invisibly returns a data frame with columns:
- pos
Position of the variable in the dataset
- variable
Variable name
- label
Variable label
- target
Indicator showing target variable
Examples
df <- data.frame(a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8)
# Show 5 before and 5 after (default)
show_variable_context("e", data = df)
# Show only 2 before and 2 after
show_variable_context("e", data = df, before = 2, after = 2)
# Show 10 before and 3 after
show_variable_context("e", data = df, before = 10, after = 3)