Create Data Frame Row by Row in R (2 Examples)

 

In this tutorial, I’ll illustrate how to construct a data frame by adding more and more rows in the R programming language.

Table of contents:

You’re here for the answer, so let’s get straight to the examples!

 

Example 1: Create Data Frame Row by Row Manually

Example 1 explains how to manually build up a data frame row by row. For this, we first have to create an empty data frame in R:

data1 <- data.frame(x1 = numeric(),    # Create empty data frame
                    x2 = character(),
                    x3 = numeric(),
                    stringsAsFactors = FALSE)
data1                                  # Print data frame to console
# [1] x1 x2 x3
# <0 rows> (or 0-length row.names)

The previous R code has constructed a data frame with zero rows and three columns. Note that we have already specified the data type of each variable.

Next, we can use square brackets (i.e. [1, ]) and the list function to insert the first row to our empty data frame.

The list that we are inserting needs to have the same length as the number of columns in our empty data frame and the data types of the list elements have to be the same as we have specified for the empty data frame (i.e. numeric, character, and numeric).

data1[1, ] <- list(5, "x", 7)          # Create first row of data frame
data1                                  # Print data frame to console
#   x1 x2 x3
# 1  5  x  7

Have a look at the previous output of the RStudio console: It shows that our updated data frame has one row.

We can now continue this process by adding another row at the second row index position:

data1[2, ] <- list(3, "aaa", 2)        # Add second row
data1                                  # Print data frame to console
#   x1  x2 x3
# 1  5   x  7
# 2  3 aaa  2

Our new data frame has two rows.

As you have seen in this example, we can append more and more rows to our data frame manually.

However, depending on the size of the final data frame this can be a very time consuming process. The next example therefore explains how to automatize the process of adding data frame rows.

 

Example 2: Create Data Frame Row by Row Using for-Loop

Example 2 shows how to construct a data frame row by row using a for-loop. For this, we first have to create an empty data frame (as in Example 1):

data2 <- data.frame(x1 = numeric(),    # Create empty data frame
                    x2 = character(),
                    x3 = numeric(),
                    stringsAsFactors = FALSE)

Now, we can run a for-loop that adds another row in each iteration of the loop:

for(i in 1:5) {                        # Initialize for-loop
 
  list_i <- c(i, letters[i], i^3)      # Create some values for this row
 
  data2[i, ] <- list_i                 # Add values at the bottom of data frame
}

Have a look at the final data frame that was created within the for-loop:

data2                                  # Print data frame to console
#   x1 x2  x3
# 1  1  a   1
# 2  2  b   8
# 3  3  c  27
# 4  4  d  64
# 5  5  e 125

Our final data frame has five rows – Looks good!

 

Video, Further Resources & Summary

I have recently published a video on my YouTube channel, which shows the R programming syntax of this article. You can find the video below.

 

The YouTube video will be added soon.

 

Furthermore, you may want to have a look at the related posts on statisticsglobe.com.

 

This article illustrated how to create a data frame row by row in R. If you have any additional questions, let me know in the comments.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top