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"))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.
Value
A data frame with columns:
- col
Column position in the data
- name
Variable name
- label
Variable label (or
""if none)
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
library(dplyr)
data(survey_data)
# Find all variables related to "trust"
find_var(survey_data, "trust")
#> col name label
#> 1 11 trust_government Trust in government (1=none, 5=complete)
#> 2 12 trust_media Trust in media (1=none, 5=complete)
#> 3 13 trust_science Trust in science (1=none, 5=complete)
# Search only in variable labels
find_var(survey_data, "satisfaction", search = "label")
#> col name label
#> 1 10 life_satisfaction Life satisfaction (1=dissatisfied, 5=satisfied)
# Use regex to find numbered items
find_var(survey_data, "^q[0-9]+", search = "name")
#> No variables found matching "^q[0-9]+".
#> [1] col name label
#> <0 rows> (or 0-length row.names)
