How to find proportions of a vector in R?

This recipe helps you find proportions of a vector in R

Recipe Objective

While carrying out a statistical analysis on a set of observations collected, finding proportions of these observations that meets a particular condition is most common. ​

To carry out this task, we will follow the following steps: ​

  1. Applying a condition on the vector which is also knwon as boolean test to get a vector of boolean values.
  2. Now passing these boolean values to the mean() function to get the proportion of all the TRUE values

In this recipe, we will discuss how to find proprtions of a vector in R discussing the above steps in details. ​

Step 1: Creating a numeric Vector

We will use a sales example in this case by creating a vector of no of sales of a certain product that took place in a period of 12 months. sales_data = c(5500, 2400, 2500, 2100, 2300, 2600, 2700, 2800, 2300, 3500, 6000, 7500)

Step 2: Apply a boolean test

Finding the sales which are greater than 5000. bool_test_results = sales_data > 5000 bool_test_results

TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE

Step 3: Finding the proportion of the vector

Find the proportion of the sales that are greater than 5000 ​

mean(bool_test_results)
0.25

This means that 25% of the sales were higher than 5000 in a period of 12 months. ​

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.

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.

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.

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.

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

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.

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 Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.