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

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Time Series Analysis with Facebook Prophet Python and Cesium
Time Series Analysis Project - Use the Facebook Prophet and Cesium Open Source Library for Time Series Forecasting in Python

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

End-to-End Speech Emotion Recognition Project using ANN
Speech Emotion Recognition using RAVDESS Audio Dataset - Build an Artificial Neural Network Model to Classify Audio Data into various Emotions like Sad, Happy, Angry, and Neutral

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