• Post author:
  • Post category:R Programming
  • Post last modified:March 27, 2024
  • Reading time:11 mins read
You are currently viewing How to Remove the First Row of DataFrame in R?

R provides a subset() function to remove the first row from the DataFrame (data.frame), you can also use the notation [] and -c() to delete the first rows. Manipulating data frames is a common task, and removing specific rows from a data frame can be achieved using various methods. In this article, I will explain several approaches to removing the first row from a data frame using different functions and packages, such as negative indexing, tail(), subset(), and dplyr. Let’s explore each method and understand how it works.

Key points-

  • Use the function tail() with the argument n set to the number of rows in the data frame minus 1.
  • Utilize negative indexing to exclude the first row when subsetting the data frame.
  • Apply the subset() function with the condition row_number() > 1 to filter out the first row.
  • Deploy the slice() function from the dplyr package, specifying the row indices starting from the second row.
  • Make use of the [-1,] notation to exclude the first row when extracting rows from the data frame.

Let’s create a DataFrame with 5 rows and 3 columns.


# Create dataframe with 5 rows and 3 columns
df = data.frame(id=c(1,2,3,4,5),
              name=c('sravan','srinu','chrisa','shivgami','jim'),
              gender=c('m','m','f','f','m'))

# Display dataframe
df

Yields below output.

remove first row in R

Using Negative Indexing

To delete the first row from R DataFrame(data.frame) you can use [] notation with the negative row index. Here, we delete the first row from the R DataFrame using the row index number. The row number starts with 1.

Syntax:


# Syntax
df[-row_index1,]

Let’s pass the first-row index with a negative sign into df[] notation, it will return the given data frame without including the first row of the data farme.


# Delete first row using negative index
df <- df[-1, ]
cat("After removing first row")
df

Yields below output.

remove first row in R

Using c() function

Alternatively, you can remove the first row from the data frame using c() function. This function creates a vector containing the row numbers you want to exclude from the data frame df. The - sign before c(...) removes that specific row from the data frame.

Syntax:


# Syntax
df[-c(row_index1),]

Let’s remove the first row from the data frame by using the c() function.


# Delete first row c() function
df <- df[-c(1), ]
df

Yields the same as the above output.

Using tail() function

The tail() function in R is used to retrieve the last n-1 rows, effectively removing the first row. When applied to a dataframe, it returns the last few rows of the dataframe. The -1 as the second argument in tail(df, -1) specifies that you want all rows except the first one.

Syntax:


# Syntax
df <- tail(df, -row_index)

Let’s pass the given data frame and negative first-row index into the tail() function to get the data frame without including the first row.


# Delete first row tail() function
df <- tail(df, -1)
cat("After removing first row")
df

Yields the same as the above output.

Using subset() function

Similarly, you can use the subset() function to select a particular row from a data frame based on the condition. This method takes two parameters data frame object and the condition. This function can ignore the rows from the data frame that fail the condition.

Syntax:


# Syntax
subset(df,condition )

The subset() function is used to filter rows based on a condition. Here, we exclude the first row.


# Delete first row subset() function
df <- subset(df, subset = rownames(df) != 1)
cat("After removing first row")
df

Yields the same as the above output.

Using the slice() from dplyr package

You can use the slice() function from the dplyr package in R to remove the first row from the dataframe. dplyr is a popular package for data manipulation in R. It provides a set of functions that make it easier to work with data frames. before going to use the slice() function we need to install dplyr packages and load it as library(dplyr).

Syntax:


# Syntax
library(dplyr)
df <- slice(df, -row_index1)

Pass the given data frame and first-row index into the slice() function, it will remove the first row from the data frame and return the modified data frame.


# Delete first row slice() from dplyr
library(dplyr)
df <- slice(df, -1)
cat("After removing first row")
df

Yields the same as the above output.

Using slice_head() from dplyr

The slice_head() function from the dplyr package removes the specified number of rows from the top. To remove the first row from the data frame use this function by specifying the number of rows with the first row index. It will return the data frame without including the first row.

Syntax:


# Syntax
library(dplyr)
df <- slice_head(df, n = -row_index1)

Let’s use the slice_head() function to remove the first row from the data frame in R.


# Delete first row slice_head() from dplyr
library(dplyr)
df <- slice_head(df, n = -1)
cat("After removing first row")
df

Yields the same as the above output.

Using dplyr with filter()

The filter() function from the dplyr package in R to remove the first row from the dataframe df. This function filters out the rows where the row number is not equal to 1. This effectively removes the first row from the dataframe.

Syntax:


# Syntax
library(dplyr)
df <- filter(df, -row_number() != 1)

Let’s use the filter() function to remove the first row from the data frame.


# Delete first row filter() from dplyr
library(dplyr)
df <- filter(df, row_number() != 1)
cat("After removing first row")
df

Conclusion

In this article, I have explained several approaches to removing the first row from a data frame using different functions and packages, such as negative indexing, c(), tail(), subset(), and dplyr with multiple examples.

Happy learning!!