Learn R Programming

sjmisc (version 2.8.10)

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,
  regex = FALSE
)

find_in_data( data, pattern, ignore.case = TRUE, search = c("name_label", "name_value", "label_value", "name", "label", "value", "all"), out = c("table", "df", "index"), fuzzy = FALSE, regex = FALSE )

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

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 stringr::regex(). Alternatively, use regex = TRUE to treat pattern as a regular expression rather than a fixed string.

ignore.case

Logical, whether matching should be case sensitive or not. ignore.case is ignored when pattern is no regular expression or regex = FALSE.

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.

regex

Logical, if TRUE, pattern is treated as a regular expression rather than a fixed string.

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). Regular expressions are supported as well, by simply using pattern = stringr::regex(...) or regex = TRUE.

Examples

Run this code
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
if (FALSE) {
library(sjPlot)
view_df(res)}

Run the code above in your browser using DataLab