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
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.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition to the video, you might read the related posts on https://statisticsglobe.com/.
- Remove Bottom N Rows from Data Frame in R
- Remove All Whitespace in Each Data Frame Column
- Conditionally Remove Row from Data Frame
- Remove Empty Rows of Data Frame
- Remove Rows with NA in R Data Frame
- Introduction to R Programming
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.
Statistics Globe Newsletter