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

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Build CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.

MLOps Project on GCP using Kubeflow for Model Deployment
MLOps using Kubeflow on GCP - Build and deploy a deep learning model on Google Cloud Platform using Kubeflow pipelines in Python

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

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

Hands-On Approach to Regression Discontinuity Design Python
In this machine learning project, you will learn to implement Regression Discontinuity Design Example in Python to determine the effect of age on Mortality Rate in Python.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.

Build CI/CD Pipeline for Machine Learning Projects using Jenkins
In this project, you will learn how to create a CI/CD pipeline for a search engine application using Jenkins.

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

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