Remove All Rows of Data Frame in R (Example)

 

On this page you’ll learn how to delete each row in a data frame in the R programming language.

The page will consist of these content blocks:

Let’s dive right in:

 

Construction of Example Data

The following data will be used as a basis for this tutorial:

data <- data.frame(x1 = letters[1:5],    # Create example data frame
                   x2 = 5:1,
                   x3 = 10:14)
data                                     # Print example data frame

 

table 1 data frame remove all rows data frame

 

As you can see based on Table 1, our example data is a data frame and has five rows and three columns. The column x1 is a character and the variables x2 and x3 are integers.

 

Example: How to Delete All Rows in a Data Frame

In this example, I’ll illustrate how to remove each row in a data frame object in order to create an empty data frame that contains only column names.

For this task, we can use square brackets and the index value 0. Have a look at the R code below:

data_empty <- data[0, ]                  # Drop all rows from data frame
data_empty                               # Print empty data frame
# [1] x1 x2 x3
# <0 rows> (or 0-length row.names)

As you can see based on the previous output of the RStudio console, we have constructed an empty data frame with zero rows that contains the variable names of our input data frame.

 

Video & Further Resources

Have a look at the following video on my YouTube channel. In the video, I’m explaining the topics of this article in a live programming session.

 

 

In addition to the video, you might read the related posts on https://statisticsglobe.com/.

 

In this article you have learned how to drop all rows in a data frame (i.e. delete all values) in the R programming language. In case you have additional questions, don’t hesitate to let me know in the comments.

 

Subscribe to the Statistics Globe 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.

Top