Learn R Programming

sjmisc (version 2.3.0)

find_var: Find variable by name or label

Description

This functions finds variables in a data frame, which variable names or variable (and value) label attribute match a specific pattern. Regular expression for the pattern is supported.

Usage

find_var(data, pattern, ignore.case = TRUE, search = c("name_label", "name_value", "label_value", "name", "label", "value", "all"), as.df = FALSE, as.varlab = FALSE)

Arguments

data
A data frame.
pattern
Character string to be matched in data. May also be a character vector of length > 1 (see 'Examples'). pattern is searched for in column names and variable label attributes of data (see get_label). pattern might also be a regular-expression object, as returned by regex, or any of stringr's supported modifiers.
ignore.case
Logical, whether matching should be case sensitive or not.
search
Character string, indicating where pattern is sought. Use one of following options:
as.df
Logical, if TRUE, a data frame with matching variables is returned (instead of their column indices).
as.varlab
Logical, if TRUE, not only column indices, but also variables labels of matching variables are returned (as data frame).

Value

A named vector with column indices of found variables (variable names are used as names-attribute); if as.df = TRUE, a tibble with found variables; or, if as.varlab = TRUE, a tibble with three columns: column number, variable name and variable label.

Details

This function searches for pattern in data's column names and - for labelled data - in all variable and value labels of data's variables (see get_label for details on variable labels and labelled data). Search is performed using the str_detect functions; hence, regular expressions are supported as well, by simply using pattern = stringr::regex(...).

Examples

Run this code
data(efc)

# find variables with "cop" in variable name
find_var(efc, "cop")

# return tibble with matching variables
find_var(efc, "cop", as.df = TRUE)

# or return "summary"-tibble with matching variables
# and their variable labels
find_var(efc, "cop", as.varlab = TRUE)

# find variables with "dependency" in names and variable labels
find_var(efc, "dependency")
get_label(efc$e42dep)

# find variables with "level" in names and value labels
res <- find_var(efc, "level", search = "name_value", as.df = TRUE)
res
get_labels(res, attr.only = FALSE)

# use sjPlot::view_df() to view results
## Not run: 
# library(sjPlot)
# view_df(res)## End(Not run)

Run the code above in your browser using DataLab