Learn R Programming

mariposa (version 0.7.3)

find_var: Find Variables by Name or Label

Description

Searches for variables in your data by matching a pattern against variable names, variable labels, or both. This is especially useful for SPSS datasets where variable names are often cryptic codes (e.g., v104, q23a_1) and the actual meaning is stored in variable labels.

Usage

find_var(data, pattern, search = c("name_label", "name", "label"))

Value

A data frame with columns:

col

Column position in the data

name

Variable name

label

Variable label (or "" if none)

Arguments

data

A data frame (typically imported from SPSS with read_spss).

pattern

A search term or regular expression to match against variable names and/or labels. Case-insensitive by default.

search

Where to search: "name_label" (default) searches both variable names and labels, "name" searches only names, "label" searches only labels.

Details

When to Use This

  • You imported an SPSS file and need to find which variable contains "trust" or "satisfaction"

  • You want to quickly identify all variables related to a topic

  • You know the German/English label text but not the variable code

Pattern Matching

The pattern argument supports regular expressions. Matching is case-insensitive. Some examples:

  • "trust" — matches "trust", "Trust", "distrust", "trustworthy"

  • "^trust" — matches only names/labels starting with "trust"

  • "^q[0-9]+" — matches variable names like q1, q23, q104

  • "zufried" — finds German labels containing "Zufriedenheit"

See Also

var_label() for getting/setting variable labels, val_labels() for value labels

Other labels: copy_labels(), drop_labels(), set_na(), to_character(), to_label(), to_labelled(), to_numeric(), unlabel(), val_labels(), var_label()

Examples

Run this code
library(dplyr)
data(survey_data)

# Find all variables related to "trust"
find_var(survey_data, "trust")

# Search only in variable labels
find_var(survey_data, "satisfaction", search = "label")

# Use regex to find numbered items
find_var(survey_data, "^q[0-9]+", search = "name")

Run the code above in your browser using DataLab