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

What is a detach function?

What is a detach function?

This recipe explains what is a detach function

0

Recipe Objective

If we want to unload or detach a package from the Rsearch path, we use detach function in R. It is an in-built function in R which also removes the R-objects (such as data.frame) available in the search path after using the attach function. ​

This recipe demonstartes how to use detach function ​

Step 1: loading required library and a dataset

# Data manipulation package library(tidyverse) # reading a dataset customer_seg = read.csv('R_198_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: Detaching a dataframe

We first attach a dataframe and then use detach function to show it's application ​

Syntax: detach(x) ​

where: x = Dataframe or matrix ​

# we first use attach 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'
# detaching a dataframe which was initially attached detach(customer_seg) # Now accessing the variable by its name Gender
Error in eval(expr, envir, enclos): object 'Gender' not found
Traceback:

Note: we cannot work with the variable name as before after using detach function ​

Step 3: Unloading a Package

# we unload the package using the same detach function detach("package:tidyverse") tidyverse::glimpse(customer_seg)
Error: 'glimpse' is not an exported object from 'namespace:tidyverse'
Traceback:

1. tidyverse::glimpse
2. getExportedValue(pkg, name)
3. stop(gettextf("'%s' is not an exported object from 'namespace:%s'", 
 .     name, getNamespaceName(ns)), call. = FALSE, domain = NA)

Note: we cannot access any functions from the tidyverse package after detaching it ​

Relevant Projects

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

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.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.