How to use for loops in R?

How to use for loops in R?

How to use for loops in R?

This recipe helps you use for loops in R


Recipe Objective

How to use a for loop in R? Loops are used to execute a particular block of code for a specified number of times for a given condition. The execution depends on boolean conditions—if the condition is true, the loop continues to execute, but if the condition is false, the loop breaks out of the execution. A for loop executes for an exact defined number of iterations and hence is called a 'definite loop' This recipe demonstrates an example of a for loop.

Step 1-Create a vector

a <- c(1:10)

Step 2- Use for loop

When the for loop is executed,following result is executed.

for (i in a) { i=i*2 print(i) } # here the loop executes the values in the vector for a given condition
"Output of the code is"

for (i in a) {
} # here the loop executes the values in the vector for a given condition 
"Output of the code is " 
[1] 2
[1] 4
[1] 6
[1] 8
[1] 10
[1] 12
[1] 14
[1] 16
[1] 18
[1] 20 

Relevant Projects

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

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

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

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.

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.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.