What is attach function?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

What is attach function?

What is attach function?

This recipe explains what is attach function

0

Recipe Objective

If we want to access variables of a data frame without actually calling the dataset, we use attach function in R. It is an in-built function in R which makes the R-objects (such as data.frame) available to the search path. This means that the dataset is searched by R and brought to the global environment when evaluating a variable. This makes the variables in the dataset are accessible by simply specifying it's name. ​

This recipe demonstartes how to use attach function ​

Step 1: loading required library and a dataset

# Data manipulation package library(tidyverse) # reading a dataset customer_seg = read.csv('R_197_Mall_Customers.csv') #summary of the dataset glimpse(customer_seg)
Observations: 200
Variables: 5
$ CustomerID              1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1...
$ Gender                  Male, Male, Female, Female, Female, Female, ...
$ Age                     19, 21, 20, 23, 31, 22, 35, 23, 64, 30, 67, ...
$ Annual.Income..k..      15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 19, ...
$ Spending.Score..1.100.  39, 81, 6, 77, 40, 76, 6, 94, 3, 72, 14, 99,...

Step 2: Accessing each variable

Gender
Error in eval(expr, envir, enclos): object 'Gender' not found
Traceback:

Note: We cannot access the varriable of the dataframe just by using it's name. To do this, we use attach() function ​

Syntax: attach(x) ​

where: x = Dataframe or matrix ​

# we use attch function to bring the dataframe in search Path attach(customer_seg) # Now accessing the variable by its name (Gender) Gender
Male Male Female Female Female Female Female Female Male Female Male Female Female Female Male Male Female Male Male Female Male Male Female Male Female Male Female Male Female Female Male Female Male Male Female Female Female Female Female Female Female Male Male Female Female Female Female Female Female Female Female Male Female Male Female Male Female Male Female Male Male Male Female Female Male Male Female Female Male Female Male Female Female Female Male Male Female Male Female Female Male Male Male Female Female Male Female Female Female Female Female Male Male Female Female Male Female Female Male Male Female Female Male Male Male Female Female Male Male Male Male Female Female Male Female Female Female Female Female Female Male Female Female Male Female Female Male Male Male Male Male Male Female Female Male Female Female Male Male Female Female Male Female Female Male Male Male Female Female Male Male Male Female Female Female Female Male Female Male Female Female Female Male Female Male Female Male Female Female Male Male Male Male Male Female Female Male Male Male Male Female Female Male Female Female Male Female Male Female Female Female Female Male Female Female Female Female Male Male Male
 Levels:
'Female' 'Male'

Alternative to attach function is the "$" operator ​

# we use "$" operator as an alternative to attach function to access the variable customer_seg$Gender
Male Male Female Female Female Female Female Female Male Female Male Female Female Female Male Male Female Male Male Female Male Male Female Male Female Male Female Male Female Female Male Female Male Male Female Female Female Female Female Female Female Male Male Female Female Female Female Female Female Female Female Male Female Male Female Male Female Male Female Male Male Male Female Female Male Male Female Female Male Female Male Female Female Female Male Male Female Male Female Female Male Male Male Female Female Male Female Female Female Female Female Male Male Female Female Male Female Female Male Male Female Female Male Male Male Female Female Male Male Male Male Female Female Male Female Female Female Female Female Female Male Female Female Male Female Female Male Male Male Male Male Male Female Female Male Female Female Male Male Female Female Male Female Female Male Male Male Female Female Male Male Male Female Female Female Female Male Female Male Female Female Female Male Female Male Female Male Female Female Male Male Male Male Male Female Female Male Male Male Male Female Female Male Female Female Male Female Male Female Female Female Female Male Female Female Female Female Male Male Male
 Levels:
'Female' 'Male'

Relevant Projects

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.