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"),
fixed = FALSE
)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.- fixed
If
TRUE,patternis searched as literal text (case-insensitive), e.g.find_var(data, "BEFRAGTE(R)", fixed = TRUE)for label text with parentheses. DefaultFALSE: regular expression. A pattern that is not a valid regular expression (e.g."(") is searched as literal text automatically, and a message points tofixed = TRUEwhen the literal text would give other matches.
Value
A data frame with columns:
- col
Column position in the data
- name
Variable name
- label
Variable label (or
""if none)
Without matches, an empty data frame is returned invisibly (with a message).
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"
Label text often contains characters with a special meaning in regular
expressions, such as parentheses: "BEFRAGTE(R)" as a regular
expression matches "BEFRAGTER". Use fixed = TRUE to search for
the text exactly as written.
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]+".
# Literal text with regex characters, e.g. parentheses in a label
find_var(survey_data, "(1=left", fixed = TRUE)
#> col name label
#> 1 8 political_orientation Political orientation (1=left, 5=right)
