Store Results of Loop in Data Frame in R (Example) | Save while- & for-Loops
In this article you’ll learn how to save the results of a loop in a data frame in R.
The article will consist of this information:
If you want to learn more about these topics, keep reading:
Example: Saving output of for-Loop in Data Frame
This Example explains how to store the results of a for-loop in a data frame.
First, we have to create a data frame with the number of rows that our final data frame will have. The following R code creates such a data frame and fills the cells with NA values. This initial data frame needs to have only one column, since this data is overwritten later on.
data <- data.frame(NA_col = rep(NA, 3)) # Creating data containing NA data # Printing data # NA_col # 1 NA # 2 NA # 3 NA
Now, we can write a for-loop that saves its results in our data frame. The following R code runs a for-loop with five iterations. In each iteration a new column is created, added to our data frame, and renamed.
for(i in 1:5) { # Head of for-loop new_col <- rep(i, 3) # Creating new variable data[ , i] <- new_col # Adding new variable to data colnames(data)[i] <- paste0("Col_", i) # Renaming new variable }
Have a look at the final output:
data # Printing updated data # Col_1 Col_2 Col_3 Col_4 Col_5 # 1 1 2 3 4 5 # 2 1 2 3 4 5 # 3 1 2 3 4 5
As you can see based on the previous output of the RStudio console, we have created a data frame consisting of three rows and five numeric columns. Each column contains the output of one iteration of our for-loop.
Note that we could use the same type of R code within while-loops or repeat-loops.
Video & Further Resources
I have recently published a video on my YouTube channel, which illustrates the R syntax of the present post. You can find the video below.
The YouTube video will be added soon.
In addition, you might want to have a look at the other articles on my website. I have published several articles about loops in R already.
- Append to Data Frame in Loop
- Store Results of Loop in Vector
- for-Loop in R
- Loops in R
- Rename a Column Name in R
- The R Programming Language
This page illustrated how to save for-loop outputs in data frames in R. In case you have any additional questions, let me know in the comments section below.
4 Comments. Leave new
Thanks a lot! very helpful!!!
Can you please explain a bit how to create new rows where 3 columns for each row. The rows represent years(2000 to 2014). So, 15 rows would be created for a chunk of codes. This is my little research, looking forward to your kind reply.
Hey Angel,
Thank you very much for the kind words!
In case you want to append rows to a data frame I would use the rbind function: https://statisticsglobe.com/rbind-in-r-example-vector-data-frame-fill-columns/
If you want to use rbind within a for-loop, your code may look like this:
Regards,
Joachim
thanks for the tutorial, i didnt used all of them, but it does inspire me how to finish my own coding!
Hey Hang,
Thank you for the kind comment. Great to hear that the tutorial was useful to you!
Regards,
Joachim