The attach() Function In R: How To Use attach() In R

attach function in r

The attach() function in R allows us to use a variable in a data frame without calling the data frame. If you are not familiar with it, then don’t worry because in this tutorial, we will show you the syntax and some of the most common usages of the attach() function. 

Attach() function in R

What does the attach() function do in R? 

The basic idea of the attach() function is to attach a data frame to the current environment. Therefore, you don’t have to include the name of the data frame when calling an object inside that data frame.

Syntax:

attach(data, pos)

Parameters

  • data: the data frame 
  • pos (optional): the position of the database

How to use attach() in R?

Let’s take a look at this example: 

We have this data set about the number of factory workers from 1851 to 1901 in Germany:

dataFrame <- data.frame(
  year = c(1851, 1861, 1871, 1881, 1891, 1901),
  maleEmployees = c(287100, 131780, 80123, 76132, 65000, 31000),
  femaleEmployees = c(190000, 160000, 60000, 50000, 40000, 30000),
  totalEmployees = c(477100, 291780, 140123, 126132, 105000, 61000),
  factories = c(225, 227, 622, 721, 625, 600)
)

dataFrame
  year maleEmployees femaleEmployees totalEmployees factories
1 1851        287100          190000         477100       225
2 1861        131780          160000         291780       227
3 1871         80123           60000         140123       622
4 1881         76132           50000         126132       721
5 1891         65000           40000         105000       625
6 1901         31000           30000          61000       600

Normally, when trying to call the year object in this data set, we have to include the name of the data frame:

dataFrame$year
[1] 1851 1861 1871 1881 1891 1901

Else, we will run into this error:

year
Error: object 'year' not found

Now, let’s attach the data frame to the current R environment using attach() function. 

attach(dataFrame)

Since we have attached the data frame, we can now call an object without calling the data frame:

attach(dataFrame)
year
[1] 1851 1861 1871 1881 1891 1901

Full code: 

dataFrame <- data.frame(
  year = c(1851, 1861, 1871, 1881, 1891, 1901),
  maleEmployees = c(287100, 131780, 80123, 76132, 65000, 31000),
  femaleEmployees = c(190000, 160000, 60000, 50000, 40000, 30000),
  totalEmployees = c(477100, 291780, 140123, 126132, 105000, 61000),
  factories = c(225, 227, 622, 721, 625, 600)
)

attach(dataFrame)
year
maleEmployees

Output:

[1] 1851 1861 1871 1881 1891 1901
[1] 287100 131780  80123  76132  65000  31000

If you want to detach an object from the current R environment, you can use the detach() function. If you want to check which objects are in the current environment, use the search() function.

Summary

In this tutorial, we have shown you the definition and application of the attach() function in R. The attach() function will allow us to access objects that are included in a data frame without calling the data frame.

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *