The attach()
method is used to access variables of a DataFrame without invoking any function or method. This method bypasses extensive built-in and directly attaches variables of a list or DataFrame to the search path.
attach(what,pos = 2L,name = deparse(substitute(what),backtick=FALSE),warn.conflicts = TRUE)
what
: It can either be database
, DataFrame
, List
, NULL or an environment.pos
: Its default value is 2L. It shows an integer position in the search index or in the database.name
: This is the label used for an attached database.warn.conflicts
: The default value is True
. If set to True
, it prints the warnings; otherwise, it hides the warnings.This method returns either a database, DataFrame, list, or NULL
.
The code below shows how the attach()
function works:
# Using the R program to illustrate# Creating a sample listRecord <- data.frame(height = c(1, 2, 3, 4, 5),width = c(6, 7, 8, 9, 0),area = c(6, 14, 24, 36, 0))# Indirectly accessing variables of the Record list# Showing the summary of the Recordsummary(Record$height)# Attaching the Record list to the search pathattach(Record)# Showing the summary of the Recordsummary(height)
height
, width
, and area
.summary(Record$height)
to print the data associated with the height
variable in theRecord
list. The $
sign specifies each variable within a list. To overcome this problem, we use the attach()
function. This includes the Record
list in the search path.attach(Record)
to attach the Record
to the current search path. We can also change the search path explicitly by invoking search()
.summary(height)
has direct access to the Record
variables.Note: We can use the
attach()
function in multiple ways according to our needs. The above example is just to give us an overview of the function.