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
Syntax:

list1  = list()

for (i in vector_indicating_no_of_elements){

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

Example:

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

[[2]]
[1] 8

[[3]]
[1] 27

[[4]]
[1] 64

[[5]]
[1] 125

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 a Multi Class Image Classification Model Python using CNN
This project explains How to build a Sequential Model that can perform Multi Class Image Classification in Python using CNN

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.

Learn to Build an End-to-End Machine Learning Pipeline - Part 2
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, incorporating Hopsworks' feature store and Weights and Biases for model experimentation.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask

Learn How to Build PyTorch Neural Networks from Scratch
In this deep learning project, you will learn how to build PyTorch neural networks from scratch.

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

Multi-Class Text Classification with Deep Learning using BERT
In this deep learning project, you will implement one of the most popular state of the art Transformer models, BERT for Multi-Class Text Classification