How to sort a vector in R?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to sort a vector in R?

How to sort a vector in R?

This recipe helps you sort a vector in R

0

Recipe Objective

Vector is a type of object or data structure in R-language. They are designed to store multiple values of same data-type. For example: if you want to store different 50 food items for each cuisine, you don't need to create 50 variables for each cuisine but just a vector of length 50 of datatype character.

Note: It can not have a combination of any two datatype. It has to be homogeneous in nature.

This recipe demonstrates how to sort a vector

Step 1: Creating a numeric vector

We use combine function "c()" to create a vector

a = c(20,2,45,16,100)

Step 2: Sorting a vector

We use sort() function to sort a vector. By default it sorts the vector in ascending order. For a character vector, the sorting takes places based on the ASCII code of the alphabets used

sort(a)
2 16 20 45 100
a
20 2 45 16 100

Note: It does not returns the sorted values back to the vector. Hence, we need to create a new vector or update the existing vector.

a = sort(a) a
2 16 20 45 100

To sort it in descending order, we add another arguement "decreasing = True" after this:

a = sort(a,decreasing = TRUE) a
100 45 20 16 2

Relevant Projects

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

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.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.