What is colon operator?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

What is colon operator?

What is colon operator?

This recipe explains what is colon operator

0

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

Relevant Projects

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

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.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

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.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.