How to Use attach() in R (With Examples)


You can use the attach() function in R to make objects in data frames accessible without actually typing the name of the data frame.

This function uses the following basic syntax:

attach(data)

The following examples show how to use this function in different scenarios with the following data frame:

#create data frame
df <- data.frame(team=c('A', 'B', 'C', 'D', 'E'),
                 points=c(99, 90, 86, 88, 95),
                 assists=c(33, 28, 31, 39, 34),
                 rebounds=c(30, 28, 24, 24, 28))

#view data frame
df

  team points assists rebounds
1    A     99      33       30
2    B     90      28       28
3    C     86      31       24
4    D     88      39       24
5    E     95      34       28

Example 1: Use attach() to Perform Calculations

Normally if we would like to calculate the mean, median, range, etc. of a column in a data frame, we would use the following syntax:

#calculate mean of rebounds column
mean(df$rebounds)

[1] 26.8

#calculate median of rebounds column
median(df$rebounds)

[1] 28

#calculate range of rebounds column
range(df$rebounds)

[1] 24 30

However, if we use attach() then we don’t even have to type out the data frame name to perform these calculations:

attach(df)

#calculate mean of rebounds column
mean(rebounds)

[1] 26.8
#calculate median of rebounds column
median(rebounds)

[1] 28
#calculate range of rebounds column
range(rebounds)

[1] 24 30

By using attach(), we’re able to reference the column name directly and R knows which data frame we’re trying to use.

Example 2: Use attach() to Fit Regression Models

Normally if we would like to fit a linear regression model in R, we would use the following syntax:

#fit regression model
fit <- lm(points ~ assists + rebounds, data=df)

#view coefficients of regression model
summary(fit)$coef

              Estimate Std. Error  t value   Pr(>|t|)
(Intercept) 18.7071984 13.2030474 1.416885 0.29222633
assists      0.5194553  0.2162095 2.402555 0.13821408
rebounds     2.0802529  0.3273034 6.355733 0.02387244

However, if we use attach() then we don’t even have to use the data argument within the lm() function to fit the regression model:

#fit regression model
fit <- lm(points ~ assists + rebounds)

#view coefficients of regression model
summary(fit)$coef

              Estimate Std. Error  t value   Pr(>|t|)
(Intercept) 18.7071984 13.2030474 1.416885 0.29222633
assists      0.5194553  0.2162095 2.402555 0.13821408
rebounds     2.0802529  0.3273034 6.355733 0.02387244

Notice that the regression results are the exact same.

Bonus: Use detach() and search()

You can use the search() function to display all of the objects that are attached in the current R environment:

#show all attached objects
search()

 [1] ".GlobalEnv"        "df"                "package:stats"    
 [4] "package:graphics"  "package:grDevices" "package:utils"    
 [7] "package:datasets"  "package:methods"   "Autoloads"        
[10] "package:base"  

And you can use the detach() function to detach an object that is currently detached:

#detach data frame
detach(df)

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Clear the Environment in R
How to Clear All Plots in RStudio
How to Print Multiple Variables on the Same Line in R

Leave a Reply

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