Learn R Programming

sjmisc (version 2.5.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, fuzzy = 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:

"name_label"

The default, searches for pattern in variable names and variable labels.

"name_value"

Searches for pattern in variable names and value labels.

"label_value"

Searches for pattern in variable and value labels.

"name"

Searches for pattern in variable names.

"label"

Searches for pattern in variable labels

"value"

Searches for pattern in value labels.

"all"

Searches for pattern in variable names, variable and value labels.

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).

fuzzy

Logical, if TRUE, "fuzzy matching" (partial and close distance matching) will be used to find pattern in data if no exact match was found. str_pos is used for fuzzy matching.

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
# NOT RUN {
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
library(sjlabelled)
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)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab