How to append output from a for loop to a list in R?

How to append output from a for loop to a list in R?

How to append output from a for loop to a list in R?

This recipe helps you append output from a for loop to a list in R


Recipe Objective

Loops are an important feature in R-language. It helps us to iterate through vectors, lists and process required functions to its elements. They help us to implement complex logic which requires a repetitive step. ​

In this recipe, we will demonstrate how to use a for loop to append it's output in a list as rows using indexing technique. ​

Steps to be follow are: ​

  1. Defining an empty dataframe
  2. Defining a for loop with iterations equal to the no of rows we want to append.
  3. Using indexing technique to append the output of one iteration to the list

list1  = list()

for (i in vector_indicating_no_of_elements){

  output = [output of one iteration]
  list1[[length(list1) + 1]] = output


Create a list of 5 elements with each element corresponding to the following function: ​

(i^3) ; where: i is the corresponding element index ​

# Defining an empty dataframe list1 = list() # Defining a for loop with 5 iterations for (i in 1:5) { output = i^3 # Using indexing techni to append the output of one iteration to the dataframe list1[[length(list1) + 1]] = output } #printing the list print(list1) ​
[1] 1

[1] 8

[1] 27

[1] 64

[1] 125

Relevant Projects

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

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.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

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 - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Loan Eligibility Prediction in Python using
In this loan prediction project you will build predictive models in Python using to predict if an applicant is able to repay the loan or not.

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.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

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

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.