How to aggregate a single column in a dataframe in R

This recipe helps you aggregate a single column in a dataframe in R

Recipe Objective

How to aggregate a single column in a dataframe in R?

Aggregation is an operation performed on a set of data to obtain a single valued output that tells us about the significance of a given dataset. A few aggregation functions are: sum, mean, median, average, etc. The following recipe demonstrates an example of how to drop null values in R.

Step 1 - Define a dataframe

df <- data.frame(x = c("A","A","A","B","B"), y = c(10,20,30,40,50), z = c(5,14,96,35,25)) print(df)

 "Dataframe :"
  x  y  z
1 A 10  5
2 A 20 14
3 A 30 96
4 B 40 35
5 B 50 25

Step 2 - Use the aggregate()

Syntax - aggregate(y ~ x, data, function) where, y - column whose value is to be aggregated x - categorical column data - dataframe function - aggregation function like - sum, mean, etc

mean_val <- aggregate(y ~ x, data = df, mean) # finding mean of a column print(mean_val)

"Output of the code is:"
  x  y
1 A 20
2 B 45

sum_val <- aggregate(y ~ x, data = df, sum) # finding sum of values in a column print(sum_val)

"Output of the code is:"
 x  y
1 A 60
2 B 90

count_val <- aggregate(y ~ x, data = df, length) # finding th ecount / length of elements in a column print(count_val)

"Output of the code is:"
 x y
1 A 3
2 B 2

{"mode":"full","isActive":false}

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.

Model Deployment on GCP using Streamlit for Resume Parsing
Perform model deployment on GCP for resume parsing model using Streamlit App.

Build an AI Chatbot from Scratch using Keras Sequential Model
In this NLP Project, you will learn how to build an AI Chatbot from Scratch using Keras Sequential Model.

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.