What is colon operator in R

This recipe explains what is colon operator in R

Recipe Objective

Colon operator (":") in R is a function that generates regular sequences. It is most commonly used in for loops, to index and to create a vector with increasing or decreasing sequence.

To look at the detailed R documention of the colon operator:

?":"

It is a binary operator i.e. it takes two arguments.

Syntax: a:b

where:

a is the starting value of sequence ; b is the end value of the sequence

It generates a sequence from a to b in steps of 1 or -1 where value of b is included in the sequence.

This recipe is going to demonstrate 2 use cases of ":" operator

  1. Creating a sequential vector
  2. For loop

1. Creating a sequential vector

#in increasing sequence vector_in = 1:9 vector_in
1 2 3 4 5 6 7 8 9
#in decreasing sequence vector_de = 1:-9 vector_de
1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9

2. For loop

It is often used with the length() function in for loop to excecute a particular function on every element of a vector.

Example: we will square the elements of the vector using ":" and for loop

#creating a function to square the elements of a vector square_ = function(x){ for (i in 1:length(x)){ print(i^2) } } ​ # giving vector_in as an arguement to get the squares of the element square_(vector_in)
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36
[1] 49
[1] 64
[1] 81

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Learn Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

End-to-End Snowflake Healthcare Analytics Project on AWS-1
In this Snowflake Healthcare Analytics Project, you will leverage Snowflake on AWS to predict patient length of stay (LOS) in hospitals. The prediction of LOS can help in efficient resource allocation, lower the risk of staff/visitor infections, and improve overall hospital functioning.

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.

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.

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.

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.

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.

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.