Home » R Tutorials » Remove First Row of Data Frame in R

Remove First Row of Data Frame in R

Using the negative indexing into the data frame, we can remove the first row of a data frame in R.

Syntax to remove the first row of the data frame

df[-1,]
where
df = data frame
Use negative indexing -1 to drop

In this tutorial, we will discuss how to remove the first row of a data frame in R using negative indexing.

Remove First Row of Data Frame using Negative Indexing

Use the negative index into the data frame to delete the row of df.

Let’s practice with an example to delete the first row of a data frame using indexing.

Create a data frame using data.frame() function in R.

# Create a data frame
sales_data <- data.frame(
  id = c(1,2,3,4,5,6),
  name = c("Ebook","Audio","Video","Course","Book","Software"),
  revenue = c(45500,53000,55200,13650,33400,13800)
)
# Prints the data frame
sales_data

Run the above R code to create the data frame and print the data frame.

In the output, as follows, the data frame contains 6 rows and 3 columns.

  id     name revenue
1  1    Ebook   45500
2  2    Audio   53000
3  3    Video   55200
4  4   Course   13650
5  5     Book   33400
6  6 Software   13800

Use the negative indexing to remove the first row of the data frame.

# Delete the first observation of data frame
sales_data <- sales_data[-1,]
# Print the data frame
sales_data

Run the R code, it will remove the first row of the data frame and prints the data frame as follows:

 id     name revenue
2  2    Audio   53000
3  3    Video   55200
4  4   Course   13650
5  5     Book   33400
6  6 Software   13800

Delete First observation of Data Frame

Let’s practice with an example to delete the first row of the data frame using the negative indexing.

Create a data frame of 6 rows and 4 columns using the list of vectors.

name <- c("Tom","Kim","Sam","Julie","Emily","Chris")
gender <- c("Male", "Male", "Male", "Female","Female","Male")
age <- c(18, 20, 17, 21,23,21)
marks <- c(72,77,65,80,85,87)

# Create a data frame using the data.frame function
student_info <- data.frame(
name,
age,
gender,
marks
)

# Print the data frame
student_info

Run the above R code to create a data frame from the list of vectors and print the data frame. The data frame has 6 rows and 4 columns.

To remove the first row from the data frame, use square bracket notation to specify the negative index.

# Delete first row of data frame
student_info <- student_info[-1,]

# print the data frame
student_info

The output of the above R code is:

  name age gender marks
2   Kim  20   Male    77
3   Sam  17   Male    65
4 Julie  21 Female    80
5 Emily  23 Female    85
6 Chris  21   Male    87

Conclusion

I hope the above article about how to remove the first row of a data frame is helpful to you.

Use negative indexing in data frame to delete row from the data frame.

Leave a Comment