WHat is tapply in R?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

# WHat is tapply in R?

WHat is tapply in R

0

## Recipe Objective

Problem: Iteration through a long list or vector using a for loop takes tremendous amount of time.

This problem is solved by using apply family of functions in R. This family of functions can be fed with many built-in functions to perform different tasks on the collection of objects such as list, vector, dataframe etc.

The family of apply functions are listed below:

1. apply()
2. lapply()
3. sapply()
4. tapply()

tapply() is a function that applies a function to subsets of a vector which is defined by another vector, usually a factor.

This recipe demonstrates how to use the tapply() using dataframe as input

Dataset description: It is the basic data about the customers going to the supermarket mall. The variable that we interested in is Annual.Income (in 1000s) and Gender.

``` # Data manipulation package library(tidyverse) ​ # reading a dataset customer_seg = read.csv('R_75_Mall_Customers.csv') ​ glimpse(customer_seg) ```
```Rows: 200
Columns: 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: Using tapply()

Using the tapply() with the following syntax:

tapply(X, INDEX , FUN)

where:

1. X = data frame or matrix ;
2. INDEX = an argument which represents the Factor column by which the subsets are created
3. FUN = function that needs to be applied on every element of the dataframe
``` # applying mean function on "Annual income" with respect to Gender factor result = tapply(customer_seg[,c("Annual.Income..k..")], customer_seg\$Gender, FUN = mean) ​ ​ # Mean annual income result ```
```Female59.25Male62.2272727272727
```

#### Relevant Projects

##### 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.

##### Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

##### Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

##### Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

##### Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

##### 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.

##### Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

##### 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.

##### 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.

##### Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.