find
find locates objects by name in R’s search path. It returns the environments where a matching object exists. This is useful for debugging namespace conflicts, tracing where a function or variable comes from, and understanding R’s search order.
find lives in the utils package, which ships with base R and is always available.
Signature
find(what, mode = "any", numeric = FALSE, simple.words = TRUE)
Arguments:
what— object name to search for (whole word match by default)mode— filter by object mode:"any","function","numeric","character", etc.numeric— ifTRUE, return search path positions instead of environment namessimple.words— ifTRUE, matchwhatas a whole word (not a regex)
Returns: A character vector of environment names, or a named numeric vector of positions.
Basic Usage
find("mean")
# [1] "package:base"
find("data.frame")
# [1] "package:base"
# Your own objects appear in .GlobalEnv
my_var <- 42
find("my_var")
# [1] ".GlobalEnv"
The search path is the chain of environments R looks through when you type a name. The default path includes your global environment, attached packages, and their namespaces.
Finding Multiple Matches
find("cor")
# [1] ".GlobalEnv" "package:stats"
# You have a 'cor' object in your workspace
# and stats::cor is also on the search path
This is the classic namespace conflict: you have an object with the same name as a package function. Use find() to diagnose it.
The numeric Argument
Set numeric = TRUE to get search path positions instead of names:
find("mean", numeric = TRUE)
# .GlobalEnv package:base
# 2 4
The numbers correspond to positions in search():
search()
# [1] ".GlobalEnv" "package:stats" "package:base" ...
# positions: 1 2 3 ...
Filtering by Mode
The mode argument restricts the search to objects of a specific type:
find("cor", mode = "function")
# [1] "package:stats"
find("cor", mode = "function", numeric = TRUE)
# package:stats
# 2
Without mode filtering, find("cor") returns both the stats function and any object named cor in your workspace.
Common mode values: "any", "function", "numeric", "character", "logical", "list", "environment".
simple.words and Regular Expressions
By default (simple.words = TRUE), find matches the name exactly as a whole word:
find("data.frame")
# [1] "package:base"
Set simple.words = FALSE to use regex matching:
find("data\\.frame$", simple.words = FALSE)
# [1] "package:base"
Regex matching is case-sensitive, even if you might expect it not to be.
Diagnosing Namespace Conflicts
The most common use of find is figuring out why a different function is running than expected:
# You think you're using dplyr::filter but getting stats::filter
library(dplyr)
find("filter")
# [1] ".GlobalEnv" "package:stats" "package:dplyr"
# stats::filter is still on the path!
# Explicitly use dplyr::filter to avoid ambiguity
# A variable in your workspace masking a package function
df <- data.frame(x = 1:5)
find("df")
# [1] ".GlobalEnv"
# The base::df function is still there but hidden
find("df", mode = "function")
# [1] "package:base"
Finding Everything in an Environment
To list all objects in a specific environment, use ls() instead:
ls("package:base")
ls(".GlobalEnv")
find locates where a specific name is defined, while ls lists every name in an environment.
Relationship to apropos
apropos() and find() are related but serve different purposes:
# apropos returns names matching a pattern
apropos("glm")
# [1] "anova.glm" "confint.glm" "extractAIC.glm" ...
# find returns where a specific object is located
find("glm")
# character(0) (no object literally named "glm")
find("anova.glm")
# [1] "package:stats"
apropos finds by partial name (regex). find finds by exact name (or whole-word match by default) and tells you where it lives.
Common Uses
Checking What a Function Really Is
find("t.test")
# [1] "package:stats"
find("t.test", mode = "function")
# [1] "package:stats"
Finding Installed Packages with an Object
find("ggplot")
# [1] "package:ggplot2"
Debugging “object not found” Errors
# You got an error that 'foo' was not found
# Check if it exists anywhere on the search path
find("foo")
# character(0) -- it doesn't exist anywhere
# Maybe you typo'd?
find("fo0") # check for misspellings
Common Pitfalls
Assuming find Returns Only Functions
find finds any object, not just functions. Use mode = "function" to filter:
x <- 42
find("x")
# [1] ".GlobalEnv"
find("x", mode = "numeric")
# [1] ".GlobalEnv"
Case Sensitivity
Unlike apropos, find is always case-sensitive:
find("Mean") # not found
find("mean") # found in package:base
Character(0) vs NULL
If an object is not found, find returns character(0), not NULL:
find("nonexistent")
# character(0)
is.null(find("nonexistent"))
# FALSE
Thinking find Searches File Paths
find searches R’s in-memory search path (environments), not files on disk. Use list.files() or dir() for filesystem searches.
See Also
- /reference/base-functions/which/ — return indices of TRUE values in a logical vector
- /reference/base-functions/match/ — find positions of elements in one vector within another
- /reference/base-functions/apply/ — apply a function over array margins
- /reference/tidyverse/purrr_keep/ — keep elements of a list that satisfy a predicate