Learn R Programming

sjmisc (version 2.7.7)

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"),
  out = c("table", "df", "index"), fuzzy = FALSE, as.df, as.varlab)

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.

out

Output (return) format of the search results. May be abbreviated and must be one of:

"table"

A tabular overview (as data frame) with column indices, variable names and labels of matching variables.

"df"

A data frame with all matching variables.

"index"

A named vector with column indices of all matching variables.

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.

as.df

Deprecated, use out = "df" instead.

as.varlab

Deprecated, use out = "table" instead.

Value

By default (i.e. out = "table", returns a data frame with three columns: column number, variable name and variable label. If out = "index", returns a named vector with column indices of matching variables (variable names are used as names-attribute); if out = "df", returns the matching variables as data frame

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 data frame with matching variables
find_var(efc, "cop", out = "df")

# or return column numbers
find_var(efc, "cop", out = "index")

# 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", out = "df")
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