What is apply function in R?

This recipe explains what is apply function in R

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()

apply() is a function that takes a matrix or dataframe as input and gives the output in vector or array by appplying a certain operation on it.

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

Step 1: Importing libraries and loading dataset

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

# Data manipulation package library(tidyverse) ​ # reading a dataset customer_seg = read.csv('R_72_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 apply()

Using the apply() with the following syntax:

apply(X, MARGIN, FUN)

where:

  1. X = data frame or matrix ;
  2. MARGIN = an argument which represents the dimension in which the operation should take place (row or column wise). 1 for row-wise and 2 for column wise ;
  3. FUN = function that needs to be applied on every element of the dataframe
# applying sum function on 2 columns "Annual income" and "spending score" result = apply(customer_seg[,c("Annual.Income..k..","Spending.Score..1.100.")], MARGIN = 2, FUN = sum) ​ result
Annual.Income..k..12112Spending.Score..1.100.10040

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

Build a Langchain Streamlit Chatbot for EDA using LLMs
In this LLM project, you will build a Streamlit Chatbot integrated with Langchain technology for natural language interactions with a SQL database, facilitating real-time visualization and insightful insights, streamlining data exploration and analysis.

Build a Multimodal RAG System using AWS Bedrock and FAISS
In this LLM RAG Project, you will learn to build a Multimodal RAG system for a restaurant aggregator app, integrating text and visuals to deliver personalized food recommendations using advanced technologies like Amazon S3, Amazon Bedrock, and FAISS.

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

Learn How to Build a Logistic Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple logistic regression model in PyTorch for customer churn prediction.

Build a Customer Support Agent using OpenAI and AzureML
In this LLM Project, you will build an intelligent customer support agent using OpenAI and Azure ML to automate ticket categorization, prioritization, and response generation.

Hands-On Approach to Regression Discontinuity Design Python
In this machine learning project, you will learn to implement Regression Discontinuity Design Example in Python to determine the effect of age on Mortality Rate in Python.

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

OSZAR »