What is the apropos() and find() functions in R?

Overview

The apropos() function in R is used to return a character vector with the names of objects matching or containing the input character partially.

The find() function, on the other hand, is used to return a location where the objects of a given name can be found.

Syntax

apropos(what, where = FALSE, ignore.case = TRUE, mode = "any")
find(what, mode = "any", numeric = FALSE, simple.words = TRUE)

Parameter

The apropos() and the find() functions take a common mandatory parameter value, what, which represents the string character to match object names against.

Optional parameter values

  • where, numeric: This takes a logical value indicating whether positions in the search list should also be determined.
  • ignore.case: Takes a logical value indicating if the search should be case-sensitive or not. It has its default value as TRUE.
  • mode: Represents the character. if not "any", only the objects whose mode equals mode are searched for.
  • simple.words: Takes a logical value. If TRUE, the what parameter is only searched as a whole word.

Return value

The apropos() function returns a character vector which is sorted by name.

The find() function returns the character vector of environment names or numerical vectors of positions with names of the corresponding environments.

How to use the apropos() function

In the code below, we’ll identify all the R objects with names containing "var" using the apropos() function. And pick one of the output results to determine the environment where the object can be found.

Code

# checking for R objects which contains "var" as its name or partially
apropos("var")

Explanation

We use the apropos() function to obtain the objects in R matching the name character "var".

From the output of the code:

[1] ".expand_R_libs_env_var" "all.vars"               "estVar"                
[4] "get_all_vars"           "globalVariables"        "var"                   
[7] "var.test"               "variable.names"         "varimax"  

We have a list of R objects matching the name character var as seen in the code above.

How to use the find() function

From the output of the code, we use the find() function to determine the location of the object "all.vars" as seen in the output of the previous code.

Code

# using the find() function to return the location of the object "all.vars"
find("all.vars")

Explanation

We use the find() function to determine the location of the R object, "all.vars". From the code’s output, the object can be located in the environment named "package:base".

Free Resources